Windows Scripting

Windows Service

Elevate permission

Disk IO

robocopy

diskpart

Directory

Environment Variable

  • Windows environment variables

  • Display environment variable value

    • PowerShell: $env:<VARIABLE_NAME>, use TAB key for auto complete.
  • Refresh environment variables, making changes take effect

    • PowerShell: If chocolatey is installed, use refreshenv.

Powershell

  • Use the old-fashioned CMD prompt

    cmd /c <CMD command>
  • The Help System

    Get-Help / help <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>"}
  • Alias (opens in a new tab)

    • Display all effective aliases

      alias

      or

      Get-Alias
    • Create new aliases

      New-Alias -Name <alias-name> -Value <alias-value>
  • Working with archives

    • Compression

      • Compress a directory that includes the root directory

        Compress-Archive -Path C:\Reference -DestinationPath C:\Archives\Draft.zip

    • Extraction

      • Extract contents of an archive into a specified directory

        Expand-Archive -LiteralPath 'C:\Archives\Draft[v1].zip' -DestinationPath C:\Reference

  • Search

    • Search file(s) by pattern

      Get-Childitem –Path <PATH> -Include <FILE_PATTERN> -Exclude *.JPG,*.MP3,*.TMP -File -Recurse

    • Search file content (grep equivalent)

      Use Select-String (opens in a new tab)

  • Clipboard interactions

  • Find the location of a command, equivalent to which in bash

    Get-Command <command> -All

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.