More PowerShell one-liner commands

In a previous article, we showed how you can use PowerShell simple commands to execute an action quickly rather than writing several lines of code. We explained how you could use the Export-CSV PowerShell cmdlet to save the output of PowerShell commands to a file. We also explained how you could use Get-EventLog with a few parameters to check who rebooted production servers. In this article, we will provide some more useful PowerShell one-liner commands.

Reading text files

Using PowerShell you can quickly read text files. PowerShell offers a file to be read by using several methods. You can read a complete line in a file or read part of it depending on your requirements. To read a complete line in a file, you can use Get-Content or GC in short. Get-Content is a very powerful cmdlet. Here are a few examples of using Get-Content cmdlet:

Let’s assume you have a text file that contains A, B, C, D, E, F as items and you would like to read the complete line. What you can do is execute these PowerShell commands:

$AVariable = Get-Content C:\Temp\Test.TXT

The output will be as shown in the screenshot below:

PowerShell one-liner commands

If you would like to read items stored in the text file and store each item in a variable for taking some actions, you can use ForEach loop. You will be required to import the file as CSV in a PowerShell variable as shown in the commands below:

$ThisCSV = Import-CSV C:\Temp\Test.TXT
ForEach {$ThisItem in $ThisCSV}
Write-Host $ThisItem

If you wish to count lines in a file, simply execute this PowerShell one-liner command:

.CSV | Get-Content).Count

You can also count words in files by executing this PowerShell one-liner command:

.CSV | Get-Content | Measure-Words).Words

In case you need to execute multiple commands in one line, you can do so by adding “;” to the end of the command. For example, the command below stops a Windows process and the second item move all TXT files from C:\Temp to E:\Temp folder.

Stop-Process –Name Notepad; Move-Item C:\Temp\
.TXT E:\Temp\

Checking to see if Active Directory partitions were backed up

You need to ensure Active Directory partitions are backed up successfully so you can restore directory objects in case of any disasters. By using a single command you can check the backup status of Active Directory partitions as shown below:

exe /Showbackup

Note that Repadmin.exe is available by default on all domain controllers and can be executed from within the PowerShell window or out of the PowerShell window.

Checking last boot up time for a domain controller

While you can check event logs as to see last boot up time for a domain controller, but using a PowerShell one-liner command it’s much easier. By just executing the command below, a local domain controller will print the last boot up time:


Collecting disabled computer accounts from Active Directory

You can use the PowerShell one-liner command below to export all Active Directory computer accounts that are disabled and then save the output to a CSV file.

Get-ADComputer -Filter {(enabled -eq $false)} -ResultPageSize 2000 -resultSetSize $null -Properties Name,OperatingSystem,SamAccountName,DistinguishedName | Export-CSV C:\Temp\AllDisabledComputers.CSV

Similarly, in case you need to export all disabled user accounts from Active Directory, executing this PowerShell command will do the trick:

Search-ADAccount -AccountDisabled -UsersOnly -ResultPageSize 2000 -resultSetSize $null | Select-Object Name, SamAccountName, DistinguishedName | Export-CSV C:\Temp\AllDisabledUsers.CSV

Collecting locked-out user accounts from Active Directory

The Search-ADAccount PowerShell cmdlet offers predefined parameters and switches that you can utilize to collect locked out user accounts in an Active Directory domain. As you can see by adding “-LockedOut” switch in the PowerShell one-liner command below, you are collecting locked out user accounts and then save the output to a CSV File. The command below fetches SamAccountName and Distinguished Name of the account so it is easier for you to identify the location of the user object.

Search-ADAccount -LockedOut -UsersOnly -ResultPageSize 2000 -resultSetSize $null | Select-Object Name, SamAccountName, DistinguishedName | Export-CSV C:\Temp\AllLockedOutUserAccounts.CSV

PowerShell one-liner commands: Short but powerful

As you can see, you don’t need to write a ton of code and scripts when these PowerShell one-liner commands can get the information you need from your systems quickly.

About The Author

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