How to Convert YAML to JSON in PowerShell

Convert YAML to JSON in PowerShell

To convert YAML to JSON in PowerShell, install the powershell-yaml module, read the file with ConvertFrom-Yaml and pass the result to ConvertTo-Json with a higher -Depth: PowerShell has ConvertTo-Json built in, but it has no YAML parser, so you need the module for real YAML files. Below I cover the module, a no-install option for … Read more

Find Users With PwdLastSet Older Than 90 Days (PowerShell)

Find users with PwdLastSet older than 90 days using PowerShell

To find users whose PwdLastSet is older than 90 days, filter Get-ADUser on PasswordLastSet, the converted version of the pwdLastSet attribute: Below I test the filter on sample accounts, handle users who must change their password, build an LDAP filter for big domains and export an audit report. My test PC isn’t on a domain, … Read more

PowerShell Write-Warning: Show, Hide and Capture Warnings

PowerShell Write-Warning with examples

PowerShell Write-Warning shows a yellow WARNING: message without stopping your script. Use it for problems the user should know about but that aren’t errors: Warnings go to their own stream, so callers can hide, capture or escalate them. Below I show each option with a small disk check function. Every example gave the same output … Read more

How to Find an Array Element by Value in PowerShell

Find an array element by value in PowerShell

To find an element in a PowerShell array by value, use -contains to check if it’s there, IndexOf() to get its position, or .Where() to return the matching items: They behave differently with upper and lower case, duplicates and objects. Below I show each one on the same city list. The output was identical in … Read more

Find Files Larger Than a Specific Size Using PowerShell

Find files larger than a specific size using PowerShell

To find files larger than a specific size with PowerShell, get the files with Get-ChildItem and filter on the Length property. PowerShell understands KB, MB and GB, so you can write sizes the way you say them: Below I run it on a sample folder, show the size units, find files in a range, include … Read more

How to Find a Carriage Return in a String in PowerShell

Find a carriage return in a string in PowerShell

To find a carriage return in a string in PowerShell, look for the escape sequence “`r”. Contains() tells you whether it’s there, and IndexOf() tells you where: Carriage returns are invisible, so they cause some of the most confusing bugs, like two strings that look the same but don’t match. Below I make them visible, … Read more

How to Find Duplicate Files Using PowerShell

Find duplicate files using PowerShell

To find duplicate files with PowerShell, hash every file with Get-FileHash and group the results by hash. Files with the same hash have the same content, whatever their names are: Below I run it on a sample folder, speed it up for big folders, catch same-name files that differ, plan a safe cleanup and compare … Read more

Cannot Convert Value to Type System.String in PowerShell (Fixed)

Fix for the PowerShell error Cannot convert value to type System.String with the hidden array cases tested

PowerShell throws “Cannot convert value to type System.String” when an advanced function’s [string] parameter receives an array instead of one value. Call it once per item, or join the items into one string: In this tutorial, I’ll show you how to reproduce cannot convert value to type system.string, find the hidden array behind it, and … Read more