18 Most Useful Powershell Commands for Windows Admins

-

|

Introduction

This guide will cover 18 most useful powershell commands for sys admins.

PowerShell commands, usually in Verb-Noun format are Cmdlets ( “command lets”) used to automate tasks in Windows. In this tutorial I will share commands required to perform common tasks in Windows. Most Windows administrator will find this tutorial both useful and handy. Let’s do it!

In this tutorial, I will use PowerShell Commands, PS, PowerShell Cmdlets and Cmdlets interchangeably.

Most Useful Powershell Commands to Find and Get Help with Cmdlets

You cannot talk about the most useful powershell commands without learning how to find them. Below are the PowerShell commands that will help you find Cmdlets.

Get-Command

The Get-Command Cmdlet is the first and most important PS a PowerShell newbie should learn and know how to use. Why? It helps you find other PowerShell Cmdlets. What command can be more important than a command that can do this?

To find all PS Commands in your computer, simply execute this command below:

Get-Command
powershell commands

Advertisements



Understanding the Results of the Get-Command Cmdlet

There are four columns in the results of the Get-Command Output

  1. CommandType: This tells you whether command is an Alias, a Cmdlet, or a Function.
  2. Name: The name is the actual command you execute.
  3. Version: This is the PowerShell version
  4. Source: The module of the PS command.

With this information, you can filter the results from Get-Command. Say you want to see PowerShell commands containing the word “EventLog”, running the command below will get the job done:

Get-Command -Name *EventLog

Notice where I added the asterisks. This is because I am aware that
“EventLog” is the “Noun” part of the Cmdlets. However, if you don’t even know you could try adding the asterisks at the beginning then try the end.

Below is the result of the pervious command.

Get-Command Parameters

Lastly, before we move on, lets discuss the parameters of the Get-Command Cmdlet.

To get all the parameters and information about the Get-Command command, execute this command below:

Get-Help Get-Command -Full

This will give you all the information regarding the Get-Command Cmdlet.

I will discuss the Get-Help Cmdlet next.

Get-Help

While the Get-Command Cmdlet finds the Cmdlet you need the Get-Help PowerShell command gives you the information you need to run the command.

The easiest wat to use the Get-Help Cmdlet is to enter Get-Help followed by the command you want information on. To find more information about the Get-EventLog Cmdlet, run the command below:

Get-Help Get-EventLog

This will give you the basic information about Get-EventLog PowerShell Command. See result below:

Some Important Parameters of the Get-Help Command

Like any other PowerShell Cmdlet, the Get-Help Cmdlet has a number of parameters. Below are the most important parameters you will need.

  1. -Detailed: The Detailed parameter gives you the command SYNTAX, PARAMTERS, ALIASES, and REMARKS.
  2. -Full: The Full gives similar information provided by the Detailed parameter with more information about each parameter
  3. -Examples: Gives examples of how to use the Cmdlet. This can be very useful if you have never used the Cmdlet before and at a loss how to proceed.
  4. -Online: Opens the online help page of the Cmdlet.
To see the parameters of a PS Cmdlet, type the Cmdlet in PS, hit the space key, type hyphen “-” followed by the tab key. As you press the tab key you will scroll through available parameters.

Most Useful Powershell Commands to Manage Files and Folders

Now that you know how to find PowerShell commands, lets get you in the hood. The next set of the most useful powershell commands are Cmdlets to help you manage files and folders.

Get-ChildItem

Gets items in a specified location. To list the folders in my drive C, I will run the command below:

Get-ChildItem c:/

This will list all the top level folders. To list all files, folders include sub-folders use the –Recurse parameter.

Pro Tip
You can combine the Get-ChildItem Cmdlet let with other Cmdlet to calculate the size of each folder in a specified directory.

Copy-Item and Move-Item

You could use the Get-ChildItem Cmdlet to list items in a folder, then pipe the result to Copy-Item Cmdlet to copy the items to a new location. The command below will do the job:

Get-ChildItem C:\Dropbox | Copy-Item -Destination C:\NewFolder
The above powershell command will only copy the top level folders and files – it will NOT copy sub-folders and files. To copy all files and folders including sub-folders, include the –Recurse parameter in the Get-ChildItem command as shown below:
Get-ChildItem C:\Dropbox -Recurse | Copy-Item -Destination C:\NewFolder

While the Copy-Item Cmdlet copies items from one location to another the Move-Item Cmdlet moves the item.

Remove-Item

This Cmdlet deletes specified items. Like the Copy-Item and Move-Item Cmdlets you could pipe the output of Get-ChildItem to Remove-Item.

Use the Remove-Item Cmdlet with caution as it can delete all files and folders in your computer including Windows files!
Pro Tip
By piping the output of Get-ChildItem to Remove-Item, you could create a simple script that will delete some log files on regular bases. You could schedule the PS script to run at specified time using Windows Scheduler.

Most Useful Powershell Commands for Reporting

There are 3 sets of PowerShell commands that you can use to export items to CVS, export to text file and to HTML files. We will discuss these 3 next.

Export-Csv

Export-Csv converts a set of string into CSV and saves in a file. This Cmdlet is very important in reporting.

To demonstrate the use of Export-CSV, run the command below:

Get-Command -Verb Export

Here is the result of the command.

most useful powershell commands

You can pipe the output of the previous command into Export-CSV to create a CVS report of the results shown in the previous image.

Here is the command to accomplish this task.

Get-Command -Verb Export | Select-Object CommandType, Name, Version, Source | Export-Csv -NoTypeInformation -Path C:\NewFolder\ExportCommands.CSV

Note that I had to include the CSV file name to the path. I also have another parameter -NoTypeInformation. To learn more about -NoTypeInformation, read this article PowerShell NoTypeInformation: Applications and Uses.

There is another Cmdlet in the previous command, Select-Object. This Cmdlet was used to specify the columns to return and export to CSV. If I excluded Select-Object the output of the CSV will contain a lot of unwanted data. Later in this tutorial, I will cover Select-Object.

For your reference, below is the output of the CSV file.

powershell commands

While this report is very similar to the output shown in the previous image, it is more useful as a report. You could send the CSV file to your boss!

Out-File

The Out-file Cmdlet sends output to a text file

The command below will export the out of the Get-Command PowerShell Cmdlet to a text file instead of a CSV:

Get-Command -Verb Export | Select-Object CommandType, Name, Version, Source | Out-File C:\NewFolder\ExportCommands.txt

Here is the result in a text file: The same report, now in a text file! How good is that!

powershell commands

What if you run this report daily and do not want to over-right the content from the previous day but rather append. This command will do the job.

Get-Command -Verb Export | Select-Object CommandType, Name, Version, Source | Out-File C:\NewFolder\ExportCommands.txt -Append

Advertisements



Most Useful Powershell Commands to Manage Processes

Another set of set of most useful powershell commands for Windows administrators are Cmdlets to manage Windows processes. Let look at them.

Get-Process

This PowerShell Cmdlet list all the processes running on a local computer . It could also get the processes running on a remote computer – with the –ComputerName parameter.

However, when you run the Get-Process PowerShell Cmdlet without any parameter, it returns all processes running on the local computer. To try this, execute the command below. The result is shown in the image below.

Get-Process

Start-Process and Stop-Process PowerShell Commands

The Get-Process PowerShell Command can work with the Start-Process and Stop-Process PowerShell Cmdlets to start or stop a process or set of processes.

Let’s say you executed the Get-Process PowerShell command but you wish to stop a process with a specific process ID 10500, the command below will do the job.

Get-Process -Id 10500 | Stop-Process
Warning!
Use the Stop-Process PowerShell Cmdlet with caution as stopping the wrong process could make your computer unstable.

To start the process, pipe the output of the Get-Process PowerShell command to the Stop-Process PowerShell Cmdlet.

Most Useful PowerShell Commands to Manage Event logs

Event log management is one of the most important tasks for Windows Administrators. So, the next set of PowerShell commands will help you manage event logs.

Get-EventLog

The Get-EventLog PowerShell Cmdlet gets events in a specified event log. You can get events in a local or remote computer. To get events from a remote computer, use the –ComputerName parameter to specify the remote computer. However, note that you will require the right permissions to access the remote computer.

To get the last 5 events logged in the System event log, execute the command below

Get-EventLog -LogName System -Newest 5 
Pro Tip
The last command could be used for troubleshooting purposes.

Clear-EventLog

As you would expect there are more event log Cmdlets but we will cover this 2 for this tutorial.

The Clear-EventLog clears all events in the specified event log. The Cmdlet can clear event logs on both local and remote computers.

The command below clears all events with name “Windows PowerShell” from the local computer

Clear-EventLog "Windows PowerShell"
To execute the command below, you need to open PowherShell as Administrator – right-click and select Run as Administrator.

Most Useful PowerShell Commands to Get Information about your Computer

If you need to collect data about computers on your network – Computer Name, BIOS Version, RAM size, Disk Information, etc – Get-WmiObject POwerShell Cmdlet is your friend! lets explore this powerful Cmdlet, shall we.

Get-WmiObject

Get-WmiObject has a parameter called –Class this allows you to specify the WMI object you wish to access. The command below will get a list of WMI classes,
Get-WmiObject -List -Class Win32*

Once you know the name of the WMI class, you can execute Get-WmiObject to return useful information from a local or remote computer. Below is a list of the most important WMI classes you may need

  • Win32_PhysicalMemory – information about available memory
  • Win32_Processor – Processor information
  • Win32_LogicalDisk – Logical disk drive information
  • Win32_DiskDrive – Physical dis information
  • Win32_OperatingSystem – Information about the operating system

To get information about operating system, run the command below:

Get-WmiObject -Class Win32_OperatingSystem

Most Useful PowerShell Commands to Connect to Remote PowerShell Sessions

You cannot discuss PowerShell commands without talking about PS remoting. As a Windows Systems Administrator you will need to remotely connect to computers using PowerShell.

Here are the commands you will need .

Enter-PSSession and Exit-PSSession PowerShell Commands

The Enter-PSSession PowerShell command allows you to interactively start a remote PS session on a single computer. When you finish with the remote computer, end the session with the Exit-PSSession command.

To open a remote PS session with a computer called Computer1, run the command below:

Enter-PSSession Computer1

Invoke-Command

While the Enter-PSSession PowerShell Cmdlet allows you to execute commands on a single remote computer, the Invoke-Command Cmdlet allows you to execute commands on one or more remote computers.

If you wish to execute Get-Process command on Computer1, Computer2, Computer3, execute this command:

Invoke-Command -ComputerName Computer1, Computer2, Computer3, -ScriptBlock {Get-Process}

New-PSSession

The New-PSSession PowerShell Cmdlet allows you to open a persistent session with a remote computer. Because the session is persistent, it is a good idea to add the remote session in a variable.

To open a persistent remote PS session on computers Computer1, Computer2, execute the command below:

$session = New-PSSession -ComputerName Computer1, Computer2

With the PS session established and stored in the $session variable, you can execute normal PowerShell commands on the remote session using the
Invoke-Command PowerShell Cmdlet.

As a final example in remote PowerShell sessions, to execute the Get-Process on the remote computers, run the command:

Invoke-Command -Session $session {$Processes = Get-Process}
I stored the results of the Get-Process command in a variable called $Processes because there are multiple computers. Storing the result in a variable makes for easy data manipulation. You could use a ForEach loop to extract and organise the data.

References and Further Reading

  1. Export-Csv
  2. Out-file
  3. Get-ChildItem
  4. Remove-Item
  5. Copy-Item and Move-Item
  6. Get-Process
  7. Get-EventLog
  8. Clear-EventLog
  9. Get-WmiObject
  10. Running Remote Commands

Quick Links to Similar Guides

  1. Powershell NoTypeInformation: Applications and Examples
  2. Get-Command in PowerShell: Applications and Uses

Advertisements



  • Was this post Helpful?
  • YesNo

LEAVE A REPLY

Please enter your comment!
Please enter your name here

FEATURED POSTS

How to Add Bullet Points in Google Slides

How to Add Bullet Points in Google Slides (Android or iOS Apps or PC)

This Itechguide teaches you how to add bullet points in Google Slides. The guide offers steps to add bullet points in Google...
How to Create a Business Facebook Page without a Personal Account

How to Create a Business Facebook Page without a Personal Account

This Itechguide teaches how to create a business Facebook page without a personal account. There could be a...
How to Wrap Text in Google Sheets

How to Wrap Text in Google Sheets (from a PC or the Apps)

This Itechguide teaches you how to wrap text in Google Sheets. The guide offers steps to wrap text in Google Sheets from...
How to Change Birthday on Facebook

How to Change Birthday on Facebook (from the App or PC)

This Itechguide teaches you how to change birthday on Facebook. The guide contains steps to change birthday on Facebook from the Facebook...
Best Thrillers on Netflix

The 10 Best Thrillers on Netflix

The thrillers on Netflix is quite a large number of movies. Knowing the best thrillers to watch can be a challenge.

ADVERTISEMENTS

TRENDING POSTS

Remote Desktop Connection

Remote Desktop Connection an Internal Error Has Occurred [Fixed]

Introduction I recently received the error message "Remote Desktop Connection an Internal Error Has Occurred". It was strange because...

Find My Samsung: Register and Use Samsung Find my Mobile

Introduction Ever wondered how you could find your Samsung phone if you lost it? Find my Samsung or Samsung...
What is the Difference Between PowerShell and CMD?

Windows Powershell vs CMD: Differences and Similarities

Introduction This short guide compares Windows PowerShell vs CMD (Windows command prompt). I will cover the history and nature...
Spotify No Longer Supports this Version of Microsoft Edge

Spotify No Longer Supports this Version of Microsoft Edge [Fixed]

Introduction When you open Spotify web player on Microsoft Edge, you may receive the error message "Spotify No Longer...
Windows 10 Won't Boot

Windows 10 Won’t Boot With Black Screen? 3 Ways to Fix It

Why Won't Windows 10 Boot Up? If your Windows 10 stops with a black screen, the first question in...

BEST OF ITECHGUIDES

7 Ways to open Control Panel in Windows 10

7 Ways to open Control Panel in Windows 10

Introduction This guide demos 7 ways to open Control Panel in Windows 10. Options to...
How to Install Windows Server 2019 from USB

How to Install Windows Server 2019 from USB

Introduction This guide demos how to install Windows Server 2019 from USB. Steps to Install...
how to setup dhcp server 2016

How to Install and Configure DHCP in Windows Server 2016

Introduction This guide walks you through how to install and setup DHCP in Windows Server 2016
powershell add ad computers to array

How to Add AD Computers to a Powershell Array

Introduction There is one way to add AD Computers to a PowerShell array. A PowerShell array is a data...
How to Create a Business Facebook Page without a Personal Account

How to Create a Business Facebook Page without a Personal Account

This Itechguide teaches how to create a business Facebook page without a personal account. There could be a...

ADVERTISEMENTS

RECENT POSTS

How to Logout of Amazon App

How to Logout of Amazon App (from Android, iPhone or Windows 10)

This Itechguide teaches you how to logout of Amazon app. The guide offers steps to logout of Amazon app for Android, for...
How to Turn off YouTube Notifications

How to Turn off YouTube Notifications (PC or YouTube Apps)

This Itechguide teaches you how to turn off YouTube notifications. The guide offers steps to turn off YouTube notifications from a PC...
How to Change YouTube Thumbnail

How to Change YouTube Thumbnail (PC or YouTube Studio App)

This Itechguide teaches you how to change YouTube thumbnail. The guide offers steps to change YouTube thumbnail from a PC or...
How to Add Bullet Points in Google Slides

How to Add Bullet Points in Google Slides (Android or iOS Apps or PC)

This Itechguide teaches you how to add bullet points in Google Slides. The guide offers steps to add bullet points in Google...
How to Change Shipping Address on Amazon

How to Change Shipping Address on Amazon

This Itechguide teaches you how to change shipping address on Amazon. The guide offers steps to change shipping address on Amazon:

MUST READ

WSUS

How to Install and Configure WSUS in Windows Server 2016

What is WSUS Windows Server 2016? WSUS Windows Server 2016 is a Microsoft Server role that allows download and...
Transfer Schema Master Role

Transfer Schema Master Role With MMC or NTDSUTIL Command Tool

Introduction There are situations when an administrator may need to transfer the schema master role from one Domain Controller...
powershell get ad group members

PowerShell Script to Get Active Directory Group Members

Introduction This guide is a hands-on step by step showing how to write a Powershell script to Get AD...
How to Copy and Paste on Facebook

How to Copy and Paste on Facebook (from the Mobile App or PC)

This Itechguide teaches you how to copy and paste on Facebook. The guide offers steps to copy and paste on Facebook from...
windows 10 lock screen timeout

How to Change Screen Time Out Setting in Windows 10

Introduction There are two easy ways to change Windows 10 lock screen timeout settings: Desktop...

LATEST DEALS

Exclusive Student Offer_Save 10% on selected Surface devices

By using this website you agree to accept our Privacy Policy and Terms & Conditions