How to Remove Whitespace from Strings in PowerShell

Remove whitespace from strings in PowerShell

To remove whitespace from strings in PowerShell, use .Trim() for spaces at the start and end, or -replace ‘\s’, ” to strip every space, tab and line break: Which one you need depends on whether the spaces inside the text matter. I tested every example in PowerShell 7 and Windows PowerShell 5.1, including the invisible … Read more

How to Add a New Line in a PowerShell String

Add a new line in a PowerShell string

To add a new line in a PowerShell string, put `n (backtick n) inside a double-quoted string. The text after it starts on the next line: Output: That covers most scripts. Below I show the other new line options, when each one matters, and how to turn spaces into new lines. I ran every example … Read more

How to Check if an Array Is Empty in PowerShell

Check if an array is empty in PowerShell

To check if an array is empty in PowerShell, test its Count property. An empty array has a Count of 0, and so does a variable that holds $null: The traps are $null on the wrong side, one-item arrays that look empty, and objects with no Count in Windows PowerShell 5.1. I’ll show where PowerShell … Read more

PowerShell Where-Object Between Two Dates (With Examples)

PowerShell Where-Object between two dates

To filter objects between two dates with PowerShell Where-Object, compare the date property against a start and an end date and join both tests with -and: That one line hides three traps: times of day on the end date, dates that are really strings, and slow filtering on big logs. I’ll walk through each with … Read more

How to Get a File Extension in PowerShell (and the Name Without It)

Get a file extension and the file name without extension in PowerShell

Every file object PowerShell hands you already knows its extension. Read the Extension property to get the file extension, and BaseName for the file name without the extension: For a path that’s only text, [System.IO.Path]::GetExtension() and GetFileNameWithoutExtension() do the same job without touching the disk. In this tutorial, I’ll show you how to get a … Read more

How to Import an Excel File in PowerShell (and Read It into an Array)

Import an Excel file in PowerShell and read it into an array

To import an Excel file in PowerShell, install the ImportExcel module and run Import-Excel. It reads the first worksheet into an array, one object per row, with the header row as property names: If Excel is installed and you’d rather skip the module, the Excel COM object gives you a two-dimensional array through UsedRange.Value2. I … Read more

How to Check If a PowerShell Module Is Installed (and List Modules)

Check if a PowerShell module is installed and list installed modules

Need to check if a PowerShell module is installed? Run Get-Module -ListAvailable -Name with the module’s name. It returns the module when it’s installed and nothing when it isn’t, so it drops straight into an if: Leave out -Name and the same command lists every installed module. Below I’ll cover both jobs, plus versions, gallery … Read more