2

I'm a bit lost. I want a powershell script that creates a shortcut linking to another powershell script. That shortcut should be able to run as administrator and the target should look like this. I made it manually like this and it works.

Target: C:\system32\windowspowershell\v1.0\powershell.exe -executionpolicy bypass -noexit "TARGETPATH\test.ps1"

This is my code, but the arguments appear behind the target path instead in front of.

Is there also a possibility to replace the default logo to a specific one? i.e. the one of powershell

Any suggestions?

Thanks!

#Read current path
function Get-ScriptDirectory {
    $Invocation = (Get-Variable MyInvocation -Scope 1).Value
    Split-Path $Invocation.MyCommand.Path
}
$installpath = Get-ScriptDirectory

#create shortcut
$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$installpath\EXE.lnk")
$Shortcut.TargetPath = """$installpath\test.ps1"""
$Shortcut.Arguments = "argumentA ArgumentB"
$Shortcut.WorkingDirectory = "$installpath"
$Shortcut.Save()
2
  • 1
    Surely the TargetPath should be C:\system32\windowspowershell\v1.0\powershell.exe and the arguments are "-executionpolicy bypass -noexit ""$installPath\test.ps1""" ? Commented Apr 27, 2018 at 7:40
  • Possible duplicate of How to create a Run As Administrator shortcut using Powershell Commented Apr 27, 2018 at 15:00

1 Answer 1

3

Icon and RunAs included

## Q:\Test\2018\04\27\SO_50057555.ps1
#Read current path
function Get-ScriptDirectory {
    $Invocation = (Get-Variable MyInvocation -Scope 1).Value
    Split-Path $Invocation.MyCommand.Path
}
$installpath = Get-ScriptDirectory
$RunScript= "Test.ps1"
$ShCutLnk = "PwSh $RunScript.lnk"

#create shortcut
$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$installpath\$ShCutLnk")
$Shortcut.TargetPath = "C:\Windows\System32\windowspowershell\v1.0\powershell.exe"
$Shortcut.IconLocation = "C:\Windows\System32\windowspowershell\v1.0\powershell.exe,0" # icon index 0
$Shortcut.Arguments = "-Nop -Executionpolicy bypass -NoExit ""$installPath\$RunScript"""
$Shortcut.WorkingDirectory = "$installpath"
$Shortcut.Save()

## Make the Shortcut runas Administrator
## Source: https://stackoverflow.com/questions/28997799/how-to-create-a-run-as-administrator-shortcut-using-powershell
$bytes = [System.IO.File]::ReadAllBytes("$installpath\$ShCutLnk")
$bytes[0x15] = $bytes[0x15] -bor 0x20 #set byte 21 (0x15) bit 6 (0x20) ON
[System.IO.File]::WriteAllBytes("$installpath\$ShCutLnk", $bytes)

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.