How to Get File Creation Date in PowerShell

Get file creation date in PowerShell

To get a file creation date in PowerShell, read the CreationTime property of the file object that Get-Item returns. Use CreationTimeUtc when you want the same value in UTC: In this tutorial, I’ll show you how to read the file creation date in PowerShell for one file and a whole folder, filter and sort by … Read more

How to Encrypt a File with a Password in PowerShell

Encrypt a file with a password in PowerShell

To encrypt a file with a password in PowerShell, turn the password into a key with PBKDF2 (Rfc2898DeriveBytes), encrypt the bytes with AES, and save the salt and IV with the file: PowerShell has no built-in Encrypt-File cmdlet, so I wrapped the full recipe in two small functions. I tested them in PowerShell 7.6 and … Read more

How to Count Files in a Folder Using PowerShell

Count files in a folder using PowerShell

To count files in a folder using PowerShell, get only the files with Get-ChildItem -File and read the Count property. Add -Recurse to include every subfolder: That’s the short answer. The count can still be wrong, though: hidden files are skipped, -Filter behaves differently in Windows PowerShell 5.1, and -Exclude doesn’t skip folders. I ran … Read more

How to Convert a Log File to HTML Using PowerShell

Convert a log file to HTML using PowerShell

To convert a log file to HTML with PowerShell, split each line into fields, turn the lines into objects and pipe them to ConvertTo-Html. Out-File saves the page: The parsing step is what makes the table readable. Below I parse a sample application log, build a basic page, then a styled report with a summary … Read more

How to Rename Files with a Date in PowerShell

Rename files with a date in PowerShell using Rename-Item

To rename files with a date in PowerShell, pipe the files to Rename-Item and build each new name in a script block from BaseName, a formatted date and Extension: Swap Get-Date for $_.LastWriteTime to stamp each file with its own date. Below I’ll walk through both, plus the duplicate-name, colon and Windows PowerShell 5.1 traps … Read more

How to Extract the Directory from a File Path in PowerShell

Extract the directory from a file path in PowerShell with Split-Path

To extract the directory from a file path in PowerShell, pass the path to Split-Path. Its default -Parent switch drops the file name and returns the folder that holds it: Below I’ll show the folder name on its own, parent folders, Get-Item, the .NET method, relative and network paths, the running script’s folder, and where … Read more

How to Copy and Rename Files in PowerShell

Copy and rename files in PowerShell with Copy-Item

To copy and rename a file in PowerShell, you don’t need two commands. Give Copy-Item the new file name as the -Destination, and it copies and renames in one step: The original stays where it was. Below I’ll cover overwrites, missing folders, bulk renames, dates, counters and the spots where PowerShell 7 and Windows PowerShell … Read more

How to Check if a String Contains Only Numbers in PowerShell

Check if a string contains only numbers in PowerShell

To check if a string contains only numbers in PowerShell, match it against an anchored regex. This returns True only when every character is a digit from 0 to 9: I use [0-9] instead of \d on purpose. Below are the traps, with real output from PowerShell 7 and 5.1. Check if a string contains … Read more

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