I've been trying to write a script that involves creating shortcuts that point to a .ps1 file. The goal of the script would be to compare two folders and for any files that appear in folder #1 but not folder #2 a shortcut would be created that would have "powershell.exe -file '$shortcutpath'" with $shortcutpath being the location of the file itself and powershell.exe -file being needed to launch the shortcut with Powershell.
This is what I have so far. P.S... There are some lines that are used to remove any unwanted files
$sourcelist = Get-ChildItem -path "C:\Users\yo\Documents\Powershell Lab\source" | Get-ItemPropertyvalue -name "name"
$shortcutlist = Get-ChildItem -path "C:\Users\yo\Documents\Powershell Lab\fixes" | Get-ItemPropertyvalue -name "name"
#compare files
$sourceNoShortcutList = Compare-Object -ReferenceObject $sourcelist -DifferenceObject $shortcutlist |Where-Object SideIndicator -eq '=>' |ForEach-Object InputObject
$reqshortcutlist = Compare-Object -ReferenceObject $sourcelist -DifferenceObject $shortcutlist |Where-Object SideIndicator -eq '<=' |ForEach-Object InputObject
#removes unmatched files from fixes folder
foreach($filename in $sourceNoShortcutList){
$fPath = Join-Path "C:\Users\yo\Documents\Powershell Lab\fixes" $fileName
Remove-Item -LiteralPath $fPath
}
foreach($filename in $reqshortcutlist){
$shortcutpath = Join-Path "C:\Users\yo\Documents\Powershell Lab\source" $fileName
$pscpath = "powershell.exe -file '$shortcutpath'"
$sclocation = "C:\Users\yo\Documents\Powershell Lab\fixes\$filename.lnk"
$WScriptObj = New-Object -ComObject ("WScript.Shell")
$shortcut = $WscriptObj.CreateShortcut($sclocation)
$shortcut.TargetPath = $pscpath
$shortcut.Save()
}
With
$pscpath = "powershell.exe -file '$shortcutpath'"
the output looks like this
powershell.exe -file 'C:\Users\yo\Documents\Powershell Lab\source\find.ps1'
swapping " and ' looks like this
powershell.exe -file "$shortcutpath"
Looking at scripts I have manually made shortcuts for they look like this
powershell.exe -file "C:\Users\yo\Documents\Powershell Lab\source\find.ps1"
very close to
powershell.exe -file 'C:\Users\yo\Documents\Powershell Lab\source\find.ps1'
I believe I need to somehow use the " around $shortcutpath but doing so quotes the name instead of the value but this could be wrong
the error I get while running the full script looks like this
Value does not fall within the expected range.
At C:\Users\yo\Documents\Powershell Lab\Scripts\scmaker\scriptshortcutmaker.ps1:23 char:1
+ $shortcut.TargetPath = $pscpath
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException
All details listed in post
The TargetPath
should be a file path without arguments. To pass arguments use the Arguments
property.
Double quotes can be used inside a double-quoted string if they are escaped using the backtick character or doubled.
$shortcut.TargetPath = "powershell.exe"
$shortcut.Arguments = "-File ""$shortcutpath"""