PowerShell command-line tips/tricks

It’s been a busy week! Had a few long travels, took the AZ-800 (Administering Windows Server Hybrid Core Infrastructure) exam, which I passed, and had a great Tech Day at the office. We had a keynote from Microsoft (Azure Arc) and multiple sessions from my colleagues, of which I attended the Azure Landing Zones and the Terraform session. Always nice to see colleagues enthusiastic about the things they are working on.

So I didn’t have time for a long blog post. This short one is about some command-line tips/tricks that I use often 🙂

Count the number of items found

For example, when searching for items, users, or folders, you sometimes need to count them. You can copy/paste the results in Notepad and see how many lines, but you can also use this:

C:\Users\HarmV> (Get-ChildItem -Path c:\temp -Filter *.exe -Recurse).count
39

You can wrap your command with parentheses, use the ‘(‘ and ‘)’ signs around it, and add “.count” behind it. This way, the output will not be shown on screen, but it will just show you the amount found.

Finding the cmdlet that you need

Sometimes you want to search for a cmdlet, but you’re unsure if it exists or how it’s formatted. You can search for it using:

C:\Users\HarmV> Get-Help *json*

Name                              Category  Module                    Synopsis
----                              --------  ------                    --------
ConvertFrom-Json                  Cmdlet    Microsoft.PowerShell.Uti… …
ConvertTo-Json                    Cmdlet    Microsoft.PowerShell.Uti… …
Test-Json                         Cmdlet    Microsoft.PowerShell.Uti… …
ConvertTo-AutopilotConfiguration… Function  WindowsAutoPilotIntune    …
Write-M365DocJson                 Function  M365Documentation         …
Update-M365DSCExchangeResourcesS… Function  Microsoft365DSC           …
New-M365DSCConfigurationToJSON    Function  Microsoft365DSC           …
Update-M365DSCSharePointResource… Function  Microsoft365DSC           …
Update-M365DSCResourcesSettingsJ… Function  Microsoft365DSC           …

By using the Get-Help cmdlet, you can search using wildcard through all your installed modules, it will show you if it’s a cmdlet or a function and in which module it’s present.

Display your command-line history

You can browse through your previous commands using the up-and-down arrow, but you can also show it using:

C:\Users\HarmV> get-history

  Id     Duration CommandLine
  --     -------- -----------
   1        2.910 Get-ChildItem -Path c:\temp -Filter *.exe -Recurse
   2        0.172 (Get-ChildItem -Path c:\temp -Filter *.exe -Recurse).count
   3        0.036 Get-History
   4        3.396 help *history*
   5        1.116 help *json*
   6        1.190 Get-Help *json*

But that’s just the history from your current session, and the complete command-line history is saved in your profile in a text file. You can retrieve the path by using:

C:\Users\HarmV> Get-PSReadLineOption | Select-Object HistorySavePath

HistorySavePath
---------------
C:\Users\HarmV\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt

In this text file, you will find the complete history of every command you entered, and my file is 14921 lines long 🙂

Searching your command-line history

In the chapter above, I showed you how to retrieve your history. If you want to search your history without opening the text file, you can use CTRL+R and start typing a part of the command that you are searching for:

C:\Users\HarmV> foreach ($line in $csv){
>> $groupname = $line.GroupName
>> $objectid = (Get-AzureADGroup | Where-Object {$_.DisplayName -eq $groupname}).ObjectId
>> Get-AzureADGroupMember -ObjectId $objectid | select DisplayName,UserPrincipalName | Export-Csv -Path "C:\Temp\Groups\testmembers.csv" -NoTypeInformation -Append
>> }
bck-i-search: get-azureadgroupm_

I started typing “Get-Azureadgroupm,” and it retrieved it from my command-line history, and it highlighted the part found.

Keep running a command until pressing CTRL-C/Break

I’m a bit impatient sometimes and want to check a status a lot but don’t want to repeat the same command the whole time, you can use a while $true loop to keep on executing a command until you stop it by typing CTRL-C. For example:

while ($true) {
Clear-Host
Get-Date
Get-MoveRequest | Where-Object {$_.Status -ne "Suspended" -and $_.Status -ne "Failed"}
Start-Sleep -Seconds 600
}

In this example, the screen is cleared, and the date is shown so that you know from what timestamp the Get-MoveRequest (Exchange migration cmdlet) is. It will wait 10 minutes and repeat the steps until you type CTRL-C to break it. You can use any command between the curly brackets and interval time.

Use Out-GridView to display and filter results

If PowerShell_ISE is installed on your system, you can use the Out-GridView cmdlet to output results in a pop-up window with a filter field. You can do this by running:

Get-ChildItem -Path c:\temp -Recurse | Out-GridView

This will output all files in c:\temp, and you can then filter the results by typing a few letters of the word you want to search for.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.