【问题标题】:Powershell to create shortcut with quoted target pathPowershell使用引用的目标路径创建快捷方式
【发布时间】:2021-03-09 02:28:52
【问题描述】:

我尝试通过 PowerShell 脚本创建一个快捷方式,并将以下值作为目标:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command "& '"C:\Program Files\Test\script.ps1"'"

这是我现有的脚本,它可以工作,但我无法在其中获得我想要的目标路径。

$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("C:\Program Files\Test\test.lnk")
$Shortcut.TargetPath = 
$Shortcut.WorkingDirectory = "C:\Program Files\Test\tool"
$Shortcut.Save()

我希望有人可以帮助我。

【问题讨论】:

    标签: windows powershell command quotes


    【解决方案1】:

    编辑 ;) 我认为这就是你想要的:

    $WshShell = New-Object -comObject WScript.Shell
    $Shortcut = $WshShell.CreateShortcut("C:\Program Files\Test\test.lnk")
    $Shortcut.TargetPath = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
    $Shortcut.Arguments = "-Command `"& 'C:\Program Files\Test\script.ps1'`""
    $Shortcut.WorkingDirectory = "C:\Program Files\Test\tool"
    $Shortcut.Save()
    

    因为这是一个脚本而不是命令,你也可以喜欢

    $Shortcut.Arguments = "-File 'C:\Program Files\Test\script.ps1'"
    

    【讨论】:

    • 我用属性值创建了一个手动快捷方式,然后它就可以工作了。但我想要一个为我创建正确快捷方式的 powershell 脚本。查看图片以查看工作快捷方式的属性:ibb.co/q1ghjHV
    • TargetPath 不是一个目录,它是一个 powershell 脚本
    • @HelloWorldIsMyMotto Nope.. TargetPath 是 executable(即 powershell.exe)的路径。 Arguments 然后保存要发送到此可执行文件的参数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-04
    • 2012-03-30
    • 2018-10-08
    • 1970-01-01
    • 2015-10-06
    • 2015-10-27
    相关资源
    最近更新 更多