1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# specify the path to your PowerShell script $ScriptPath = "C:\test\test.ps1" # create a lnk file $shortcutPath = [System.IO.Path]::ChangeExtension($ScriptPath, "lnk") $filename = [System.IO.Path]::GetFileName($ScriptPath) # create a new shortcut $shell = New-Object -ComObject WScript.Shell $scut = $shell.CreateShortcut($shortcutPath) # launch the script with powershell.exe: $scut.TargetPath = "powershell.exe" # skip profile scripts and enable execution policy for this one call # IMPORTANT: specify only the script file name, not the complete path $scut.Arguments = "-noprofile -executionpolicy bypass -file ""$filename""" # IMPORTANT: leave the working directory empty. This way, the # shortcut uses relative paths $scut.WorkingDirectory = "" # optinally specify a nice icon $scut.IconLocation = "$env:windir\system32\shell32.dll,162" # save shortcut file $scut.Save() # open shortcut file in File Explorer explorer.exe "/select,$shortcutPath" |