Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DilanLivera/1c12e5e2737c2cf0fec859792d5d8730 to your computer and use it in GitHub Desktop.
Save DilanLivera/1c12e5e2737c2cf0fec859792d5d8730 to your computer and use it in GitHub Desktop.
Windows Command shell and PowerShell commands

Windows Command shell and PowerShell commands

The following is from Windows Commands document.

Windows has two command-line shells: the Command shell and PowerShell. Each shell is a software program that provides direct communication between you and the operating system or application, providing an environment to automate IT operations.

The Command shell was the first shell built into Windows to automate routine tasks, like user account management or nightly backups, with batch (.bat) files. With Windows Script Host, you could run more sophisticated scripts in the Command shell. For more information, see cscript or wscript. You can perform operations more efficiently by using scripts than you can by using the user interface. Scripts accept all commands that are available at the command line.

PowerShell was designed to extend the capabilities of the Command shell to run PowerShell commands called cmdlets. Cmdlets are similar to Windows Commands but provide a more extensible scripting language. You can run both Windows Commands and PowerShell cmdlets in PowerShell, but the Command shell can only run Windows Commands and not PowerShell cmdlets.

Command shell commands

Please refer to the Windows Commands document to find a list of windows Command shell commands.

List all the connected Wifi network connections

netsh wlan show profile

Use the following command to find the more details about the connection(e.g. wifi connection password).

netsh wlan show profile "name_of_the_wifi_connection" key=clear

How to trace the path that an Internet Protocol (IP) packet takes to its destination

>tracert google.com

Tracing route to google.com [142.250.70.142]
over a maximum of 30 hops:

  1     2 ms     1 ms     1 ms  192-168-1-1.x.com.au [192.168.1.1]
  2    10 ms    11 ms    14 ms  10-20-24-23.x.com.au [10.20.24.23]
  3    11 ms    10 ms    11 ms  27-31-160-135.static.x.com.au [27.31.160.135]
  4    12 ms    11 ms    11 ms  74.125.51.92
  5    12 ms    12 ms    13 ms  142.250.233.93
  6    12 ms    11 ms    11 ms  142.250.230.159
  7    10 ms    10 ms    10 ms  mel04s01-in-f14.1e100.net [142.250.70.142]

Trace complete.

How to Use TRACERT to Troubleshoot TCP/IP Problems in Windows - Microsoft Support

Find the machine's hostname

>HOSTNAME
ABCD-01

Find the machine's serial number

>WMIC BIOS GET SERIALNUMBER
SerialNumber
ABCD123

Check if a PORT is open

netstat -a -b | findstr "<port_number>"

I had to run the cmd as an Administrator

You could also use the following.

 netstat -aon
 netstat -aon | findstr "<port_number>"

Find the ports that are reserved

netsh int ipv4 show excludedportrange protocol=tcp

Delete and Reserve the range you need

netsh int ipv4 delete excludedportrange protocol=tcp startport=<port_number> numberofports=<number_of_ports>
netsh int ipv4 add excludedportrange protocol=tcp startport=<port_number> numberofports=<number_of_ports>

You "may" need to reboot and/or restart docker and other services

PowerShell commands

How to find out which process is listening on a TCP port on Windows?

Get-Process -Id (Get-NetTCPConnection -LocalPort YourPortNumberHere).OwningProcess

Eg.

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
-------  ------    -----      -----     ------     --  -- -----------
    205      74    63640       5136              7764   0 prism-cli-win

PowerShell RestMethod do a heartbeat/health check

Invoke-RestMethod 'https://localhost:5017/heartbeat' -Method 'GET' | ConvertTo-Json

How to get the public IP

(Invoke-WebRequest ifconfig.me/ip).Content

Convert to base 64

[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("(very-secret-secret)"))

Set the title on your Terminal Tab

$Host.UI.RawUI.WindowTitle = "New Title"

Get information about all IIS websites

The following is from Microsoft Docs: Get-IISSite document.

Get-IISSite
Name             ID   State      Physical Path                  Bindings
----             --   -----      -------------                  --------
Default Web Site 1    Started    %SystemDrive%\inetpub\wwwroot  http *:80: 
PattiFul         2    Stopped    C:\inetpub\PattiFul            http *:8080: 
                                                                http *:8033: 
FTPSite          3               C:\inetpub\ftproot             ftp *:21: 
DavidChe         4    Started    c:\                            http *:8088: 
MyNewSite        6555 Started    C:\inetpub\wwwroot             http *:8099: 
                                                                http *:8022:

Recursively delete folders in the specified directory and its subdirectories

Note The following command is useful to delete node_modules, bin and obj folders easily.

Get-ChildItem -Path "C:\path\to\folder" -Include "folder_to_be_removed" -Recurse | Remove-Item -Force -Recurse

# remove the node_modules folders in C:\repos directory and its subdirectories
Get-ChildItem -Path "C:\repos" -Include "node_modules" -Recurse | Remove-Item -Force -Recurse

Delete all the NuGet packages in the default NuGet cache

Remove-Item -Recurse -Force $env:USERPROFILE\.nuget\packages\*

Windows Management Instrumentation (WMI) and Common Information Model (CIM) cmdlets

Working with WMI - PowerShell | Microsoft Learn

Add a bin directory to the PATH environment variable

$env:Path += ";C:\path\to\bin"

# or
$env:Path = "C:\path\to\bin;" + $env:Path

# view the value
echo $env:Path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment