Skip to content

Instantly share code, notes, and snippets.

@jakelevi1996
Last active February 12, 2021 17:39
Show Gist options
  • Select an option

  • Save jakelevi1996/1f919ab70b995d3c43a0ab99c462d627 to your computer and use it in GitHub Desktop.

Select an option

Save jakelevi1996/1f919ab70b995d3c43a0ab99c462d627 to your computer and use it in GitHub Desktop.
Useful Powershell commands

Useful Powershell commands

This is a record of things I have found useful in Powershell. It is expected to grow over time.

Viewing information about physical network connections

The Powershell command Get-NetConnectionProfile "gets a connection profile associated with one or more physical network adapters. A connection profile represents a network connection".

With no arguments provided, for each physical network adapter, this provides various pieces of information, such as Name, InterfaceAlias, InterfaceIndex, NetworkCategory, etc.

These pieces of information can be used to find out more about a particular network connection, such as the IP address; say the InterfaceIndex = 13, then the IP address for that network connection can be found using the command Get-NetIPAddress -InterfaceIndex 13.

To change aspects of a network connection, use the Powershell command set-NetConnectionProfile, for example set-NetConnectionProfile -interfacealias "Ethernet 2" -NetworkCategory Private.

Changing the command-line prompt

The command line prompt can be changed by defining a function called prompt, as described in this Stack Overflow answer. EG:

PS C:\Users\Jake\Documents> function prompt{"$ "}
$ date

24 April 2020 11:48:52


$

Adding parameters to a powershell script

Parameters can be added to a Powershell script using the param function, which allows parameters to be given a name, and optionally also a default value and data-type. For example, if the following is saved in a file called script.ps1:

param($a=3, $b=4)

$c = $a + $b

echo "a = $a, b = $b, a + b = $c"

It can be called from the command line in various different ways:

$ ./script   
a = 3, b = 4, a + b = 7
$ ./script 20
a = 20, b = 4, a + b = 24
$ ./script 20 30
a = 20, b = 30, a + b = 50
$ ./script -b 23
a = 3, b = 23, a + b = 26
$ ./script -b 23 -a 42
a = 42, b = 23, a + b = 65
$ powershell -file script.ps1 -b 123
a = 3, b = 123, a + b = 126
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment