使用 Powershell 修改应用程序快捷方式时出错
Error when modifying app shortcut using Powershell
我有以下 Powershell 代码,使 Chrome 在单击任务栏上的固定图标时始终使用默认配置文件打开:
$shortcutPath = "$($Env:APPDATA)\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar\Google Chrome.lnk"
$shell = New-Object -COM WScript.Shell
$shortcut = $shell.CreateShortcut($shortcutPath) ## Open the lnk
$shortcut.TargetPath
if ($shortcut.TargetPath.EndsWith("chrome.exe")) {
$shortcut.TargetPath = """$($shortcut.TargetPath)"" --profile-directory=""Default"""
$shortcut.Save() ## Save
}
执行时,if
语句抛出以下错误:
Value does not fall within the expected range.
At line:2 char:31
+ $shortcut.TargetPath = """$($shortcut.TargetPath)"" --profile-direc ...
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException
为什么会出现上述错误?以及如何解决?谢谢!
您不应将参数添加到 TargetPath
属性,而是在快捷方式的 Arguments
属性:
中设置这些参数
$shortcutPath = "$($Env:APPDATA)\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar\Google Chrome.lnk"
$shell = New-Object -COM WScript.Shell
$shortcut = $shell.CreateShortcut($shortcutPath) ## Open the lnk
Write-Host "TargetPath = $($shortcut.TargetPath)"
Write-Host "Arguments = $($shortcut.Arguments)"
if ($shortcut.Arguments -ne '--profile-directory="Default"' ) {
$shortcut.Arguments = '--profile-directory="Default"'
$shortcut.Save() ## Save
}
# Important, always clean-up COM objects when done
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($shortcut)
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($shell)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
我有以下 Powershell 代码,使 Chrome 在单击任务栏上的固定图标时始终使用默认配置文件打开:
$shortcutPath = "$($Env:APPDATA)\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar\Google Chrome.lnk"
$shell = New-Object -COM WScript.Shell
$shortcut = $shell.CreateShortcut($shortcutPath) ## Open the lnk
$shortcut.TargetPath
if ($shortcut.TargetPath.EndsWith("chrome.exe")) {
$shortcut.TargetPath = """$($shortcut.TargetPath)"" --profile-directory=""Default"""
$shortcut.Save() ## Save
}
执行时,if
语句抛出以下错误:
Value does not fall within the expected range.
At line:2 char:31
+ $shortcut.TargetPath = """$($shortcut.TargetPath)"" --profile-direc ...
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException
为什么会出现上述错误?以及如何解决?谢谢!
您不应将参数添加到 TargetPath
属性,而是在快捷方式的 Arguments
属性:
$shortcutPath = "$($Env:APPDATA)\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar\Google Chrome.lnk"
$shell = New-Object -COM WScript.Shell
$shortcut = $shell.CreateShortcut($shortcutPath) ## Open the lnk
Write-Host "TargetPath = $($shortcut.TargetPath)"
Write-Host "Arguments = $($shortcut.Arguments)"
if ($shortcut.Arguments -ne '--profile-directory="Default"' ) {
$shortcut.Arguments = '--profile-directory="Default"'
$shortcut.Save() ## Save
}
# Important, always clean-up COM objects when done
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($shortcut)
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($shell)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()