Category filter
Script to Create a New Folder and Add Shortcut on Windows
Saving too many files on your desktop may slow down the system and make it harder to organize files. In addition, files on the desktop are not as secure as the files in libraries like ‘My Documents’ when you use System Restore or run file-based backup programs. Therefore, it is recommended to create shortcuts on your desktop as a quick way to access files stored on your hard drive. With Hexnode, IT admins can seamlessly execute custom scripts remotely on Windows devices.
PowerShell Script
1 2 3 4 5 6 7 8 |
$path = "C:\" New-Item .\EpicFolder -ItemType Directory –Force $wshshell = New-Object -ComObject WScript.Shell $desktop = [System.Environment]::GetFolderPath('Desktop') $lnk = $wshshell.CreateShortcut($desktop+"\ShortcutName.lnk") $lnk.TargetPath = "c:\EpicFolder" $lnk.Save() Write-Host "Folders and Shortcut created!" |
This PowerShell code sets the target for a shortcut called “ShortcutName.lnk” to ” C:\EpicFolder” and saves it to the Desktop.
$path
– Environment variable where applications look for executables which means it can make or break a system or utility installation.
New-Item
– Creates files and folders.
ItemType
– Parameter specifies that the new item is a directory, not a file or other file system object.
Force
-Parameter lets you create a file in the profile path, even when the directories in the path do not exist.
New-Object
creates the object and sets each property value and invokes each method.
ComObject
, or plain COM, increases the range of PowerShell activities.
WScript.Shell
– Creates an object that is an instance of the Windows shell, allowing you to run commands against the Windows shell.
WshShell
is a generic name for a powerful object that enables you to query and interact with various aspects of the Windows shell.