- 1 year ago
how to create several shortcuts at the same time for all .exe files in a directory using powershell
To create shortcuts for all .exe
files in a directory using PowerShell, you can use the following script:
powershell$sourceDir = "C:\Path\to\directory" # Replace with your source directory
$shell = New-Object -ComObject WScript.Shell
$targetDir = $shell.SpecialFolders("Desktop")
$exeFiles = Get-ChildItem -Path $sourceDir -Filter "*.exe" -File
foreach ($file in $exeFiles) {
$shortcut = $shell.CreateShortcut("$targetDir\$($file.BaseName).lnk")
$shortcut.TargetPath = $file.FullName
$shortcut.Save()
}
In this script, replace "C:\Path\to\directory"
with the path to your source directory containing the .exe
files.
The script uses the WScript.Shell
COM object to create shortcuts on the desktop. It retrieves the special folder path for the desktop using $shell.SpecialFolders("Desktop")
. Then, it uses Get-ChildItem
to get all the .exe
files in the specified directory.
For each .exe
file, the script creates a shortcut using $shell.CreateShortcut()
. The target path of the shortcut is set to the full path of the .exe
file, and then the shortcut is saved using $shortcut.Save()
.
When you run the script, it will create shortcuts for all the .exe
files in the specified directory on your desktop. The shortcuts will have the same names as the corresponding .exe
files, but with the .lnk
extension.
Note: Depending on your system's security settings, you may need to run PowerShell with administrator privileges to create shortcuts on the desktop.