How to Get the File Modified Date in PowerShell (and Change It)

Get a file's modified date in PowerShell with Get-Item and LastWriteTime

A file’s modified date lives in its LastWriteTime property. Grab the file with Get-Item and read that property: That’s the same value File Explorer shows in the Date modified column. CreationTime and LastAccessTime sit right next to it on the same object. In this tutorial, I’ll show you how to get the file modified date … Read more

How to Convert a Hex String to a Byte Array in PowerShell

Convert a hex string to a byte array in PowerShell

To convert a hex string to a byte array in PowerShell 7, call [System.Convert]::FromHexString(). Windows PowerShell 5.1 doesn’t have that method, so there you convert each pair of hex digits with [Convert]::ToByte($pair, 16): Below I show both, then a function that handles spaces, dashes and 0x prefixes, checks the input, and turns the bytes back … Read more

How to Check if a String Contains Special Characters in PowerShell

Check if a string contains special characters in PowerShell

Need to check if a string contains special characters in PowerShell? Use -match with a negated character class. It returns True as soon as one character that isn’t a letter or digit shows up: The catch is deciding what “special” means. Spaces, underscores and accented letters like é all change the answer, and the -like … 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