Alias Common Windows Tools for Quick Access and Improved Functionality
Aliasing tools is a common practice on Linux and there is no reason not to do the same on Windows. A simple PowerShell script can make hard to remember commands quick and easy to access and can be customized to your use case. Save all your alias scripts to a folder and add that folder to your path variable to be able to call scripts from anywhere in the CLI!
$env:Path += ";$C:\Script_Directory"
superCopy.ps1
Robocopy is a great tool, but there are a lot of switches and the commands can get long. Most of the time I use Robocopy I want to do the same action. This script will copy a directory including subfolders, exclude items that are already in the destination directory unless the source item is newer, use restartable mode to survive minor network interruption, and limit retries to two with a two second wait between retries. Modify as needed. ss64.com has a full list of options: https://ss64.com/nt/robocopy.html
These are the options I use:
/s copy subfolders /xo exclude older files /z restartable /r:2 retries:2 /w:2 wait between retries:2 seconds /A-:SH remove system and hidden attributes (This solves the invisibiles files issue where robocopy may set the system and hidden attributes.)
#https://ss64.com/nt/robocopy.html
$Log = "$PSScriptRoot\results.txt"
Remove-Item -Force $Log -ErrorAction Ignore
$robo_source = $args[0]
$robo_dest = $args[1]
if (!$robo_source) {
$robo_source = Read-Host -Prompt "Enter Source Directory [$robo_source] "
}
if (!$robo_dest) {
$robo_dest = Read-Host -Prompt "Enter Destination [$robo_dest] "
}
robocopy /s /xo /z /r:2 /w:2 /A-:SH $robo_source $robo_dest | Tee-Object -FilePath $Log
ra.ps1
If you use Windows Remote Assistance often, you know that it takes a few clicks to get what you need in the GUI. Skip those steps and send a remote assistance offer quicky to a user’s PC by calling msra.exe from a script.
Remove-Variable Hostname -ErrorAction Ignore
$Hostname = $args[0]
while (!$Hostname) {
$Hostname = Read-Host -Prompt "Enter Hostname [$Hostname] "
}
msra /offerra $Hostname
re.ps1
You can alias Remote Desktop in a similar way by calling mstsc.exe from a script.
Remove-Variable Hostname -ErrorAction Ignore
$Hostname = $args[0]
while (!$Hostname) {
$Hostname = Read-Host -Prompt "Enter Hostname [$Hostname] "
}
mstsc /v:$Hostname
getSerial.ps1
gwmi win32_bios is an easy command to get the PCs serial number from BIOS. We can alias with a script to add more functionality. This script will get the serial number of a local, remote PC across network, or multiple remote PCs across network–requires WinRM enabled for remote PCs. Output is written to the console and the serial is copied to the clipboard.
$Log = "$PSScriptRoot\results.txt"
Remove-Item -Force $Log -ErrorAction Ignore
Remove-Variable Hostname -ErrorAction Ignore
$Hostname = $args[0]
if (!$Hostname) {
$Hostname = Read-Host -Prompt "Enter Hostname [$Hostname] "
}
if (!$Hostname) {
$Target = "self"
$Hostname = $env:computername
}
foreach ($thing in $Hostnames){
if ($Target -eq "self"){
$CmdOut = gwmi win32_bios
Set-Clipboard $CmdOut.SerialNumber
Write-Host
Write-Host Serial Number has been copied to Clipboard.
}
else {
$CmdOut = Invoke-Command -ComputerName $thing -ScriptBlock {gwmi win32_bios}
}
Write-Output ********************** >> $Log
Write-Host Hostname: $CmdOut.PSComputerName
Write-Output Hostname: $CmdOut.PSComputerName >> $Log
Write-Host Serial Number: $CmdOut.SerialNumber
Write-Output Serial Number: $CmdOut.SerialNumber >> $Log
Write-Host BIOS Version: $CmdOut.SMBIOSBIOSVersion
Write-Output BIOS Version: $CmdOut.SMBIOSBIOSVersion >> $Log
Write-Output ********************** >> $Log
}
getOS.ps1
Any “show” command that gets info from Windows we can alias in a similar way as getSerial. For example, if we want the OS version this will do it for a local or remote PC.
$Log = "$PSScriptRoot\results.txt"
Remove-Item -Force $Log -ErrorAction Ignore
Remove-Variable Hostname -ErrorAction Ignore
$Hostname = $args[0]
if (!$Hostname) {
$Hostname = Read-Host -Prompt "Enter Hostname [$Hostname] "
}
if (!$Hostname) {
$Target = "self"
$Hostname = $env:computername
}
foreach ($thing in $Hostname){
if ($Target -eq "self"){
$wmiOS = Get-WmiObject -Class Win32_OperatingSystem
$OS = $wmiOS.caption
Write-Host $OS
Write-Output $OS >> $Log
}
else {
$wmiOS = Invoke-Command -ComputerName $thing -ScriptBlock {Get-WmiObject -Class Win32_OperatingSystem}
$OS = $wmiOS.caption
Write-Host $OS
Write-Output $OS >> $Log
}
}
laps.ps1
If you are using LAPS (Local Administrator Password Solution) you can query passwords with Get-AdmPwdPassword. Wouldn’t it be nice if the resulting passwords were automatically copied to clipboard?
$Log = "$PSScriptRoot\results.txt"
Remove-Item -Force $Log -ErrorAction Ignore
Remove-Variable Hostname -ErrorAction Ignore
Remove-Variable CmdOut -ErrorAction Ignore
$Hostname = $args[0]
if (!$Hostname) {
$Hostname = Read-Host -Prompt "Enter Hostname [$Hostname] "
}
if (!$Hostname) {
$Target = "self"
$Hostname = $env:computername
}
foreach ($thing in $Hostname){
Get-AdmPwdPassword -Computername "$thing" | Tee-Object -Append -FilePath $Log
$CmdOut = Get-AdmPwdPassword -Computername "$thing"
Set-Clipboard $CmdOut.Password
Write-Host ""
Write-Host Password has been copied to clipboard.
Remove-Variable CmdOut -ErrorAction Ignore
}
ix.ps1
For commands that you will use often it makes sense to make an alias script. But sometimes you need to run a command that you don’t need often enough to bother with a making a script. This single script will take any command as a parameter and run it against a remote PC. Again, this requires WinRM.
$Log = "$PSScriptRoot\results.txt"
Remove-Item -Force $Log -ErrorAction Ignore
Remove-Variable Hostname -ErrorAction Ignore
Remove-Variable Command -ErrorAction Ignore
$Hostname = $args[0]
$Command = $args[1]
if (!$Hostname) {
$Hostname = Read-Host -Prompt "Enter Hostname [$Hostname] "
}
if (!$Hostname) {
$Target = "self"
$Hostname = $env:computername
}
if (!$Command) {
$Command = Read-Host -Prompt "Enter Command [$Command] "
}
[String]$Command = $Command
[ScriptBlock]$sb = [ScriptBlock]::Create($Command)
foreach ($thing in $Hostnames){
if ($Target -eq "self"){
$CmdOut = Invoke-Command -ScriptBlock $sb
Set-Clipboard $CmdOut
Write-Host
Write-Host Output has been copied to Clipboard.
}
else {
$CmdOut = Invoke-Command -ComputerName $thing -ScriptBlock $sb
}
Write-Host
$thing
$CmdOut
Write-Output ********************** >> $Log
Write-Output $thing >> $Log
Write-Output $CmdOut >> $Log
Write-Output ********************** >> $Log
}
ixx.ps1
Sometimes you may need an interactive session on a remote PC. Alias Enter-PSSession to make it easier to use.
Remove-Variable Hostname -ErrorAction Ignore
$Hostname = $args[0]
while (!$Hostname) {
$Hostname = Read-Host -Prompt "Enter Hostname [$Hostname] "
}
Enter-PSSession $Hostname
Further Reading
Robocopy on SS64
https://ss64.com/nt/robocopy.html
How to prevent Robocopy from hiding your files and how to fix it when it does
https://blog.coeo.com/how-to-prevent-robocopy-from-hiding-your-files
Local Administrator Password Solution
https://www.microsoft.com/en-us/download/details.aspx?id=46899
Windows Remote Management
https://docs.microsoft.com/en-us/windows/win32/winrm/portal