Short but sweet: Useful PowerShell one-liner commands

Microsoft PowerShell is a great scripting platform. PowerShell is not only used to manage Windows operating systems, but Microsoft has also extended the capability of PowerShell script to Azure Cloud. One of the biggest advantages of using PowerShell is that you do not need to write several lines of code to accomplish a simple task. Traditionally, you had to write many lines of code in VB scripting language to accomplish simple tasks. For example, if you need to check services on Windows computers and save the output to a file, you will write several lines of code that includes writing a method to open a text file and then use another line of code to save the output to the text file. PowerShell has changed the way scripting works. Microsoft has put a lot of effort into ensuring administrators can work with PowerShell scripting without acquiring much knowledge on the scripting. In this article, we will explain some useful PowerShell one-liner commands that can be helpful in daily use.

Saving output to a file

One of the easiest things PowerShell offers is the ability to save any output to a CSV file. You can use Export-CSV PowerShell cmdlet at the end of a PowerShell command to save the output to a CSV file of your choice. For example, in the PowerShell one-liner command below, you are collecting the status of Windows services on the local computer, but rather than showing the output in the PowerShell screen window you are saving the output to a CSV file. For example, these commands save the output to a CSV file:


Get-Service | Export-CSV C:\Temp\AllServices.CSV –NoTypeInfo
Get-User –Filter ‘Name –Like “*John”’ | Export-CSV C:\Temp\AllUsers.CSV –NoTypeInfo
Get-ChildItem –Path C:\Windows\System32 | Export-CSV C:\Temp\AllFiles.CSV -NoTypeInfo


Checking who rebooted a production server

Let’s say one of the production servers got rebooted unexpectedly and you would like to find out who rebooted it and when the server got rebooted. In PowerShell, you can take a look at the event log using the PowerShell one-liner command shown below. You don’t need to write a bunch of lines in a script and then run the script. Here is how you do it.

Get-EventLog –Log System –Newest 100 | Where-Object {$_.EventID –eq ‘1074’} | FT MachineName, UserName, TimeGenerated -AutoSize

The above command checks the System event log and searches for Event ID 1074 and then prints the machine name, username, and time the event got generated. If you would like to save the output to a CSV file, simply use Export-CSV cmdlet as shown in the command below:

Get-EventLog –Log System –Newest 100 | Where-Object {$_.EventID –eq ‘1074’} | FT MachineName, UserName, TimeGenerated –AutoSize | Export-CSV C:\Temp\AllEvents.CSV -NoTypeInfo

Checking to see if a KB is installed on a Windows machine

powershell one-liner

If you would like to search for a hotfix on a local or remote computer, you can use Get-HotFix PowerShell cmdlet as shown in this command:

Get-HotFix –ID KB2877616

And to query the hotfix information on the remote computer, simply add –Computername parameter as shown in the command below:

Get-HotFix –ID KB2877616 –Computername WindowsServer1.TechGenix.com

Back up all production Group Policy Objects

As we stated earlier, PowerShell offers quick one-liner commands. If you would like to backup all production Group Policy Objects (GPOs) in an Active Directory environment, use Backup-GPO PowerShell cmdlet as it is highlighted in the command below:

Backup-GPO –All –Path C:\Temp\AllGPO

Check if all Domain Controllers are Global Catalog Servers

In most of the Active Directory production environments, all domain controllers are designated as global catalog servers. The Global Catalog servers help in finding information in the Active Directory quickly. If you need to check whether all domain controllers are acting as global catalog servers or not, run this command:

Get-ADDomainController –Filter * | Select Hostname, IsGlobalCatalog

And to export the output to a CSV file, execute this command:
Get-ADDomainController –Filter * | Select Hostname, IsGlobalCatalog | Export-CSV C:\Temp\AllDomainControllerStatus.CSV -NoTypeInfo

PowerShell one-liner commands will save you time

By using these PowerShell one-liner commands, you get the information you need from your systems quickly. And as we showed you, you can use Export-CSV at the end of the command to save the output in a CSV file. If you are well versed with PowerShell — or even if you are not — you will save a lot of time by not having to write lengthy code.

About The Author

1 thought on “Short but sweet: Useful PowerShell one-liner commands”

  1. Hello Team Genix,
    I’m Rico and I’m an entry level system admin. I was wondering if anyone could assist me with a few scripts. I need a script that I can use to run software on a remote system using my system through PowerShell, I need a script that will query information that I need from active directory and place it on my desktop as an Excel document how many , machines I have, anything on my network for life cycle purposes. Any, help would be greatly appreciated thanks and god bless.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top