Hi I want to create several shortcuts at same time using powershel and something like this
Get-ChildItem -Path D:\something\ -Include *.exe -File -Recurse -ErrorAction SilentlyContinue
get the results and generate shortcuts(.lnk files) for all .exe files
(.exe is just one example of file type)
Can u help me? thx
To create shortcuts of all your .exe
files in a directory, you can do the following:
.exe
files in a directory. Similar to what you have done already with Get-ChildItem
.foreach
or Foreach-Object
here. BaseName
from files. This means getting test
from test.exe
. We need this to make the shortcut file..lnk
extension. We can use Join-Path
here to make this path. Demonstration:
$sourcePath = "C:\path\to\shortcuts"
$destinationPath = "C:\path\to\destination"
# Create COM Object for creating shortcuts
$wshShell = New-Object -ComObject WScript.Shell
# Get all .exe files from source directory
$exeFiles = Get-ChildItem -Path $sourcePath -Filter *.exe -Recurse
# Go through each file
foreach ($file in $exeFiles)
{
# Get executable filename
$basename = $file.BaseName
# Create shortcut path to save to
$shortcutPath = Join-Path -Path $destinationPath -ChildPath ($basename + ".lnk")
# Create shortcut
$shortcut = $wshShell.CreateShortcut($shortcutPath)
# Set target path of shortcut to executable
$shortcut.TargetPath = $file.FullName
# Finally save the shortcut to the path
$shortcut.Save()
}