Windows Service
-
CMD
Elevate permission
Disk IO
robocopy
diskpart
Directory
-
CMD
-
dir /xDisplays the short names generated for
non-8.3file names, which can be used to avoid spaces in paths. -
UNC paths are not supported
-
-
Powershell
cmd /c dir /x
Environment Variable
Windows environment variables Reference
Recognized Environment Variables (opens in a new tab)
Powershell
PowerShell Reference
-
Microsoft Learn - PowerShell Module Browser (opens in a new tab)
Search all PowerShell modules and cmdlets from Microsoft - just start typing in the box.
Set the value of an environment variable
$env:<VARIABLE_NAME> = "<VARIABLE_VALUE>"Get the value of an environment variable
# Use `TAB` key for auto complete
$env:<VARIABLE_NAME>Refresh environment variables, making changes take effect
PowerShell: Ifchocolateyis installed, userefreshenv.
Use the old-fashioned CMD prompt
cmd /c <CMD command>The Help System
Get-Help <command-name>or
help $env:<command-name>Sends output to an interactive table in a separate window
Out-GridView (opens in a new tab)
Get-Service | Out-GridViewPaginate standard output
Get-Service | more<SPACE>: Next page<ENTER>: Next lineq: Quit
Transpose output of a table
Get-Service | Format-ListPrettify output as a table
-
Format-Table(opens in a new tab)-AutoSize: avoid truncation-GroupBy <column-name>- Examples
Get-Service -Name win* | Format-Table -AutoSize -GroupBy status
Sort output
Sort-Object [-Descending]Examples:
Get-Process | Sort-Object idFilter output
Where-Object (opens in a new tab)
-
Examples
-
Get-Service | Where-Object {$_.<column-name> -eq "<column-value>"}
-
Display all effective aliases
aliasor
Get-AliasAbout Alias (opens in a new tab)
Create new aliases
New-Alias -Name <alias-name> -Value <alias-value>Compress a directory that includes the root directory
Compress-Archive -Path $path -DestinationPath $destinationPathExtract contents of an archive into a specified directory
Expand-Archive -LiteralPath $archivePath -DestinationPath $destinationPathSearch file(s) by pattern
Example:
Get-ChildItem -Path $path -Include $filePattern -Exclude *.JPG,*.MP3,*.TMP -File -RecurseSearch file content (grep equivalent)
Use Select-String (opens in a new tab)
Pipe stdin to clipboard (copy)
$env:ComputerName | Set-ClipboardGet clipboard content (paste)
How To Copy Paste Content via Clipboard in PowerShell (opens in a new tab)
Networking
-
Excluded port range
Messages like
Ports are not available: listen tcp 0.0.0.0:<port>: bind: An attempt was made to access a socket in a way forbidden by its access permissions.could mean the specified port is being excluded instead of being used.Use
netsh interface ipv4 show excludedportrange protocol=tcp, you will get:Protocol tcp Port Exclusion Ranges Start Port End Port ---------- -------- 1031 1130 1131 1230 1231 1330 1331 1430 1431 1530 1561 1660 2363 2462 2463 2562 2563 2662 2663 2762 2763 2862 2863 2962 5357 5357 50000 50099 * 55500 55599 * - Administered port exclusions.Run
net stop winnatandnet start winnatto restartwinnatservice.Now the port should be available, but if you are disconnected, you need to reboot.
Run
netsh interface ipv4 show excludedportrange protocol=tcp, your port is probably not excluded anymore.
Cheatsheet
Open a file with its associated program
CMD or Powershell
start %filename%Find the location of a command
Get-Command <command> -Allor
where %command%Replace text in a file with line separator
(Get-Content $input_path -Raw) -replace $pattern, $replacement | Set-Content $output_pathExample:
# Replace `\n` with line separator and `\t` with a real tab
(Get-Content input_file.txt -Raw) -replace "\\n","`r`n" -replace "\\t","`t" | Set-Content output_file.txt