How to Convert CER to PEM Using PowerShell

Convert CER to PEM using PowerShell

To convert a CER file to PEM in PowerShell 7, load it as an X509Certificate2 and call ExportCertificatePem(). In Windows PowerShell 5.1, Base64-encode the bytes and wrap them in BEGIN and END CERTIFICATE lines yourself: Linux servers, NGINX, Apache and most cloud consoles want PEM, while Windows usually exports .cer files. Below I check which … Read more

PowerShell Copy-Item: Copy Files and Folders (With Examples)

PowerShell Copy-Item with examples

Copy-Item is the PowerShell cmdlet that copies files and folders. Give it a -Path and a -Destination, and add -Recurse when you’re copying a folder with everything inside it: Output: That’s the whole idea. The details matter, though, because Copy-Item has a few habits that surprise people, like creating a file where you expected a … Read more

PowerShell Get-Date Format ISO 8601 (o, s, u, UTC and Parsing)

PowerShell Get-Date format ISO 8601

To format a date as ISO 8601 in PowerShell, use Get-Date with the round-trip format specifier o. It returns a string with the date, the time to seven decimal places and the UTC offset: On a PC set to Eastern time, that looks like 2026-10-26T09:30:15.2500000-04:00. Use Get-Date -Format s for a shorter stamp without fractions … Read more

How to Check if a Variable Exists in PowerShell

Check if a variable exists in PowerShell

To check if a variable exists in PowerShell, test its path on the Variable: drive. Test-Path returns True when the variable is defined, even if it holds $null, and False when it was never created: Get-Variable with -ErrorAction SilentlyContinue does the same job. The one check that doesn’t work is $city -eq $null, because a … Read more

How to Create a Variable with Multiple Values in PowerShell

Create a variable with multiple values in PowerShell

To create a variable with multiple values in PowerShell, assign a comma-separated list to it. PowerShell turns the list into an array, and every value stays inside that one variable: Arrays aren’t the only option, though. When each value needs a label, a hashtable fits better, and when each item is a whole record, an … Read more

How to Create a File If It Doesn’t Exist in PowerShell

Create a file if it does not exist in PowerShell

To create a file if it does not exist in PowerShell, check the path with Test-Path -PathType Leaf and call New-Item -ItemType File only when the check comes back False: That pattern never touches a file that’s already there. The details matter, though, because the obvious shortcut, New-Item -Force, wipes an existing file to 0 … Read more

How to Split Large Text Files with PowerShell (Lines, Size, CSV)

Split large text files with PowerShell

To split large text files with PowerShell, read the file in batches with Get-Content -ReadCount and write each batch to its own numbered file: Each pass of ForEach-Object gets an array of up to 100,000 lines, so the script never holds the whole file in memory. That’s the quick version. Below I split a 500,000-line … Read more

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 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