Foreward
- Powershell Commands Cheat Sheet Examples
- Powershell Network Commands Cheat Sheet Download
- Powershell Network Commands Cheat Sheet Examples
- Powershell Command Cheat Sheet Printable
- Azure Powershell Commands Cheat Sheet
This cheat sheet will be updated when Microsoft releases new information throughout PowerShell’s development lifecycle. PowerShell includes commands called cmdlets that add functionality to the core foundation, while providing a means to upgrade/update cmdlets to further augment functionality in the future versions. Pipe the output to another command Get-Service Get-Member Essential Commands To get help on any cmdlet use get-help Get-Help Get-Service To get all available cmdlets use get-command Get-Command To get all properties and methods for an object use get-member Get-Service Get-Member Creating Objects To create an instance of a com object.
I haven't located a good cheat sheet for basic PowerShell commands to manage your Network Card/IP stack, so I thought I should make one. If you like this please share it with your mom because I'm sure she's been waiting to set a static IP address via PowerShell for some time. Also you should call her more.
Assumptions
- Windows 2012/Windows 8 or higher
- Administrator access to the machine in question
- Powershell ran as administrator on the machine in question
CMDLet List:
Most of the commands used to manipulate IP settings can be found by typing Get-Command -Module NetTCPIP
To find out more about any given command, type Get-Help <cmdlet name>Common Tasks
Here are walkthroughs of some of the more common tasks you may want to perform.
List Network Adapters
Gets a list of all adapters in the machine; you'll need to know your adapter name or index # (both listed) for some of the commands below.Change an Adapter Friendly Name
You may want to consider changing the friendly name of the adapter you intend to manipulate. By giving it shorter, more meaningful name you'll have an easier time going forward. Use the old name you got from the last command. Syntax is Rename-NetAdapter -Name '<Current Name>' -NewName <'New Name'>Get the Current IP Address
Gets all IPv4 addresses on the machine; you'll almost always have multiple as your loopback interface (127.0.0.1) will be listed. Ignore that guy.Optionally, you can specify -InterfaceAlias <friendly name> or -InterfaceIndex <index #> to limit the command to a single adapter.
Assign a Static IP Address to your Network Adapter
This command will set the one and only (overwriting what is there) IP address for the specified network adapter. You also can (and should) set the subnet mask with this command. The subnet mask is set via CIDR using the -PrefixLength; see the link for more info about CIDR, but if you're not familiar with CIDR it is likely that you want -PrefixLength 24 which translates to 255.255.255.0 meaning the first three octets are the network while the last is the host. Syntax is New-NetIPAddress -InterfaceAlias <name> -IPAddress <IP address> -PrefixLength <CIDR> -DefaultGateway <Gateway IP> . You can substitute -InterfaceIndex <index #> for -InterfaceAlias .Note: You will get an error if you already have a static IP address with a default gateway. To fix this problem see 'Delete Existing Static IP' below and then try again.
Note2: We're not using 'Set-NetIPAddress' here because it doesn't allow you to set a default gateway. BOOOO.
Set DNS Servers for your Adapter
To look up names you'll need to set DNS server(s). Syntax is Set-DNSClientServerAddress -InterfaceAlias <name> -ServerAddresses ('<IP Address 1','IP Address 2') . You can set as many DNS servers as you like. You can substitute -InterfaceIndex <index #> for -InterfaceAlias .Set a Default Gateway
It's generally easier to set the default gateway as part of the New-NetIPAddress command above, but if you want to set one separately see 'Set a Static Route' below.Delete Existing Static IP (to prep for a new)
This is a two step process; you need to delete the IP, then the gateway. No need to worry about the DNS servers here as it works to overwrite them with the command above. You will need to know the IP address you want to delete first; use get-netipaddress (above) to get it (write it down to use below if necessary). You'll then need to know the NextHop of the gateway. To get this, use the get-netroute command and write down the entry(ies) that have a nexthop of the gateway you intend to remove (see screenshot).The syntax for these commands are
Powershell Commands Cheat Sheet Examples
Remove-NetAddress <IPAddress> -Confirm:$False
Remove-NetRoute -NextHop <Gateway IPAddress> -Confirm:$False
Note: If you have multiple routes set with that default gateway it will delete them all. If you haven't manually set routes, don't worry about it (you just have the one).
Set Your Adapter to Use DHCP
Powershell Network Commands Cheat Sheet Download
This is another two step process; first set the desired adapter IP/Gateway to DHCP then set the DNS servers to pull from DHCP as well.The syntax for these commands are:
Set-NetIPInterface -InterfaceAlias <name> -Dhcp Enabled
Set-DNSClientServerAddress -InterfaceAlias <name> -ResetServerAddress
You can substitute -InterfaceIndex <index #> for -IterfaceAlias if you prefer.
Note: If you have a static gateway set you'll need to perform the second step 'Remove-NetRoute' from the step above as well.
Advanced Tasks
Here are walkthroughs of some of the more common tasks you may want to perform. My assumption here is that you know what you want to do so I won't be discussing the details of what each of these means.
Add/Delete a Static Route
Add: (use -RouteMetric to specify metric or -PolicyStore to control persistence through reboots)Add default route:
Delete: (while this command is very specific you can be more generic; see above)
Test Network Connectivity (Ping)
Test-Connection replaces ping.exe. In addition to the ping functionality Test-Connection supports authentication (if firewall is set accordingly), multiple targets in a single command, running as a job, as well as more detailed returns.Assign a DNS Suffix
This is the domain under which your IP will be registered and under most circumstances will be used to append to hostname searches. Note this is per-adapter. You can substitute InterfaceIndex for InterfaceAlias if you like.Assign an Additional IP Address to your NIC
If you want to add another IP (usually only applicable on a server)References/More Information
TechNet: Net TCP/IP Cmdlets in Windows PowerShellTechNet: DNS Client Cmdlets in Windows PowerShell
TechNet: Network Adapter Cmdlets
You did it, hug a puppy!
-->
Because TCP/IP is the most commonly used network protocol, most low-level network protocoladministration tasks involve TCP/IP. In this section, we use PowerShell and WMI to do thesetasks.
Listing IP Addresses for a Computer
To get all IP addresses in use on the local computer, use the following command:
The output of this command differs from most property lists, because values are enclosed in braces:
To understand why the braces appear, use the Get-Member
cmdlet to examine the IPAddressproperty:
The IPAddress property for each network adapter is actually an array. The braces in the definitionindicate that IPAddress is not a System.String value, but an array of System.Stringvalues.
Listing IP Configuration Data
To display detailed IP configuration data for each network adapter, use the following command:
The default display for the network adapter configuration object is a very reduced set of theavailable information. For in-depth inspection and troubleshooting, use Select-Object
or aformatting cmdlet, such as Format-List
, to specify the properties to be displayed.
In modern TCP/IP networks you are probably not interested in IPX or WINS properties. You can use theExcludeProperty parameter of Select-Object
to hide properties with names that begin with'WINS' or 'IPX'.
This command returns detailed information about DHCP, DNS, routing, and other minor IP configurationproperties.
Pinging Computers
You can perform a simple ping against a computer using by Win32_PingStatus. The followingcommand performs the ping, but returns lengthy output:
A more useful form for summary information a display of the Address, ResponseTime, and StatusCodeproperties, as generated by the following command. The Autosize parameter of Format-Table
resizesthe table columns so that they display properly in PowerShell.
A StatusCode of 0 indicates a successful ping.
You can use an array to ping multiple computers with a single command. Because there is more thanone address, use the ForEach-Object
to ping each address separately:
You can use the same command format to ping all of the computers on a subnet, such as a privatenetwork that uses network number 192.168.1.0 and a standard Class C subnet mask (255.255.255.0).,Only addresses in the range of 192.168.1.1 through 192.168.1.254 are legitimate local addresses (0is always reserved for the network number and 255 is a subnet broadcast address).
To represent an array of the numbers from 1 through 254 in PowerShell, use the statement 1..254.A complete subnet ping can be performed by generating the array and then adding the values onto apartial address in the ping statement:
Note that this technique for generating a range of addresses can be used elsewhere as well. You cangenerate a complete set of addresses in this way:
Retrieving Network Adapter Properties
Earlier, we mentioned that you could retrieve general configuration properties using theWin32_NetworkAdapterConfiguration class. Although not strictly TCP/IP information, networkadapter information such as MAC addresses and adapter types can be useful for understanding what isgoing on with a computer. To get a summary of this information, use the following command:
Assigning the DNS Domain for a Network Adapter
To assign the DNS domain for automatic name resolution, use the SetDNSDomain method of theWin32_NetworkAdapterConfiguration. Because you assign the DNS domain for each network adapterconfiguration independently, you need to use a ForEach-Object
statement to assign the domain toeach adapter:
The filtering statement IPEnabled=$true
is necessary, because even on a network that uses onlyTCP/IP, several of the network adapter configurations on a computer are not true TCP/IP adapters;they are general software elements supporting RAS, PPTP, QoS, and other services for all adaptersand thus do not have an address of their own.
You can filter the command by using the Where-Object
cmdlet, instead of using theGet-CimInstance
filter.
Performing DHCP Configuration Tasks
Modifying DHCP details involves working with a set of network adapters, just as the DNSconfiguration does. There are several distinct actions you can perform by using WMI, and we willstep through a few of the common ones.
Determining DHCP-Enabled Adapters
To find the DHCP-enabled adapters on a computer, use the following command:
To exclude adapters with IP configuration problems, you can retrieve only IP-enabled adapters:
Retrieving DHCP Properties
Because DHCP-related properties for an adapter generally begin with DHCP
, you can use the Propertyparameter of Format-Table
to display only those properties:
Enabling DHCP on Each Adapter
To enable DHCP on all adapters, use the following command:
You can use the Filter statement IPEnabled=$true and DHCPEnabled=$false
to avoid enabling DHCPwhere it is already enabled, but omitting this step will not cause errors.
Releasing and Renewing DHCP Leases on Specific Adapters
The Win32_NetworkAdapterConfiguration class has ReleaseDHCPLease and RenewDHCPLeasemethods. Both are used in the same way. In general, use these methods if you only need to release orrenew addresses for an adapter on a specific subnet. The easiest way to filter adapters on a subnetis to choose only the adapter configurations that use the gateway for that subnet. For example, thefollowing command releases all DHCP leases on adapters on the local computer that are obtaining DHCPleases from 192.168.1.254:
The only change for renewing a DHCP lease is to use the RenewDHCPLease method instead of theReleaseDHCPLease method:
Note
When using these methods on a remote computer, be aware that you can lose access to the remotesystem if you are connected to it through the adapter with the released or renewed lease.
Releasing and Renewing DHCP Leases on All Adapters
You can perform global DHCP address releases or renewals on all adapters by using theWin32_NetworkAdapterConfiguration methods, ReleaseDHCPLeaseAll and RenewDHCPLeaseAll.However, the command must apply to the WMI class, rather than a particular adapter, becausereleasing and renewing leases globally is performed on the class, not on a specific adapter.
Powershell Network Commands Cheat Sheet Examples
You can get a reference to a WMI class, instead of class instances, by listing all WMI classes andthen selecting only the desired class by name. For example, the following command returns theWin32_NetworkAdapterConfiguration class:
You can treat the entire command as the class and then invoke the ReleaseDHCPAdapterLease methodon it. In the following command, the parentheses surrounding the Get-CimInstance
andWhere-Object
pipeline elements direct PowerShell to evaluate them first:
You can use the same command format to invoke the RenewDHCPLeaseAll method:
Creating a Network Share
To create a network share, use the Create method of Win32_Share:
You can also create the share by using net share
in PowerShell on Windows:
Removing a Network Share
Powershell Command Cheat Sheet Printable
You can remove a network share with Win32_Share, but the process is slightly different fromcreating a share, because you need to retrieve the specific share to be removed, rather than theWin32_Share class. The following statement deletes the share TempShare:
In Windows, net share
works as well:
Connecting a Windows Accessible Network Drive
The New-PSDrive
cmdlets creates a PowerShell drive, but drives created this way are available onlyto PowerShell. To create a new networked drive, you can use the WScript.Network COM object. Thefollowing command maps the share FPS01users
to local drive B:
,
On Windows, the net use
command works as well:
Azure Powershell Commands Cheat Sheet
Drives mapped with either WScript.Network or net use
are immediately available to PowerShell.