locked
Create Windows Shortcut With PowerShell - HELP! RRS feed

  • Question

  • Hey, I am trying to create a new shortcut that runs a PowerShell script against the local machine. The problem I am having is that when the Scripts amends the TargetPath it includes the " " (quotation marks) and the shortcut will not run as a result. How can I exclude these? I have tried [regex] escape with no joy. Thanks

    Script:

    $shell=New-Object -ComObject WScript.Shell
    $StartShortcut=$shell.CreateShortcut("C:\Users\$env:username\Desktop\PS_Scripts\Start Services - This Server.lnk")
    $StartShortcut.TargetPath = "%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -file D:\powershell\Start-Services.ps1 -computernames $env:computername"
    $StartShortcut.IconLocation = "%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe"
    $StartShortcut.WorkingDirectory ="%HOMEDRIVE%%HOMEPATH%"
    $StartShortcut.TargetLocation = "v1.0"
    $StartShortcut.Save()

    Friday, October 17, 2014 9:15 AM

Answers

  • This is closer to what you want:

    $scname=[environment]::GetFolderPath('desktop')+'\StartServices.lnk'
    $sysdir=[environment]::SystemDirectory
    $shell=New-Object -ComObject WScript.Shell
    
    $sc=$shell.CreateShortcut($scname)
    $sc.TargetPath="$sysdir\WindowsPowerShell\v1.0\powershell.exe"
    $sc.Arguments="-file C:\scripts\StartServices.ps1 -computernames $env:computername"
    $sc.IconLocation="$sysdir\WindowsPowerShell\v1.0\powershell.exe, 0"
    $sc.WorkingDirectory="$env:HOMEDRIVE$env:HOMEPATH"
    $sc.Save()
    


    ¯\_(ツ)_/¯

    • Proposed as answer by jrv Friday, October 17, 2014 10:23 AM
    • Marked as answer by JimAll Friday, October 17, 2014 12:50 PM
    Friday, October 17, 2014 10:20 AM

All replies

  • This is closer to what you want:

    $scname=[environment]::GetFolderPath('desktop')+'\StartServices.lnk'
    $sysdir=[environment]::SystemDirectory
    $shell=New-Object -ComObject WScript.Shell
    
    $sc=$shell.CreateShortcut($scname)
    $sc.TargetPath="$sysdir\WindowsPowerShell\v1.0\powershell.exe"
    $sc.Arguments="-file C:\scripts\StartServices.ps1 -computernames $env:computername"
    $sc.IconLocation="$sysdir\WindowsPowerShell\v1.0\powershell.exe, 0"
    $sc.WorkingDirectory="$env:HOMEDRIVE$env:HOMEPATH"
    $sc.Save()
    


    ¯\_(ツ)_/¯

    • Proposed as answer by jrv Friday, October 17, 2014 10:23 AM
    • Marked as answer by JimAll Friday, October 17, 2014 12:50 PM
    Friday, October 17, 2014 10:20 AM
  • try:

    $shell=New-Object -ComObject WScript.Shell
    $StartShortcut=$shell.CreateShortcut("$env:HOMEDRIVE$env:HOMEPATH\Desktop\PS_Scripts\Start Services - This Server.lnk")
    $StartShortcut.TargetPath = "$env:SystemRoot\system32\WindowsPowerShell\v1.0\powershell.exe"
    $StartShortcut.Arguments = " -file D:\powershell\Start-Services.ps1 -computernames $env:computername"
    $StartShortcut.IconLocation = "$env:SystemRoot\system32\WindowsPowerShell\v1.0\powershell.exe"
    $StartShortcut.WorkingDirectory ="$env:HOMEDRIVE$env:HOMEPATH"
    $StartShortcut.Save()

    Arguments are passed separately..


    Sam Boutros, Senior Consultant, Software Logic, KOP, PA http://superwidgets.wordpress.com (Please take a moment to Vote as Helpful and/or Mark as Answer, where applicable) _________________________________________________________________________________ Powershell: Learn it before it's an emergency http://technet.microsoft.com/en-us/scriptcenter/powershell.aspx http://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx

    Friday, October 17, 2014 10:36 AM
  • Thanks Sam
    Friday, October 17, 2014 1:01 PM
  • Thank you jrv, a few tweaks to make it work on my machine and it worked fine. One thing however is it opens in a normal CMD window. Any way of making it open in a PS window e.g Lucia Console font and a different width (120)

    Thanks again!

    Friday, October 17, 2014 1:01 PM