Windows CLI

Windows Service

Elevate permission

Disk IO

robocopy

diskpart

Directory

Environment Variable

Windows environment variables Reference

Recognized Environment Variables (opens in a new tab)

Powershell

PowerShell Reference

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: If chocolatey is installed, use refreshenv.

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-GridView

Paginate standard output

Get-Service | more
  • <SPACE>: Next page
  • <ENTER>: Next line
  • q: Quit

Transpose output of a table

Get-Service | Format-List

Prettify output as a table

Sort output

Sort-Object [-Descending]

Examples:

Get-Process | Sort-Object id

Filter output

Where-Object (opens in a new tab)

  • Examples

    • Get-Service | Where-Object {$_.<column-name> -eq "<column-value>"}

Display all effective aliases

alias

or

Get-Alias

About 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 $destinationPath

Extract contents of an archive into a specified directory

Expand-Archive -LiteralPath $archivePath -DestinationPath $destinationPath

Search file(s) by pattern

Example:

Get-ChildItem -Path $path -Include $filePattern -Exclude *.JPG,*.MP3,*.TMP -File -Recurse

Search file content (grep equivalent)

Use Select-String (opens in a new tab)

Pipe stdin to clipboard (copy)

$env:ComputerName | Set-Clipboard

Get 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 winnat and net start winnat to restart winnat service.

    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> -All

or

where %command%

Replace text in a file with line separator

(Get-Content $input_path -Raw) -replace $pattern, $replacement | Set-Content $output_path

Example:

# 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