How to Convert MBR to GPT Using PowerShell

Convert MBR to GPT using PowerShell

To convert MBR to GPT from PowerShell without losing data, run Windows’ built-in MBR2GPT tool from an elevated window, validate first, then convert, and switch the firmware to UEFI: MBR2GPT only handles the system disk, the one Windows boots from. Below I check the partition style, test a disk against MBR2GPT’s rules, cover BitLocker and … Read more

How to Write an XML Object to a File in PowerShell

Write an XML object to a file in PowerShell

To write an XML object to a file in PowerShell, call the Save() method of the [xml] document with a full path. It writes indented XML that other programs can read: Two details trip people up: relative paths in Save(), and piping XML to Out-File. Below I cover both, plus building XML from scratch, controlling … Read more

How to Convert HTML to JSON in PowerShell

Convert HTML to JSON in PowerShell

To convert HTML to JSON in PowerShell, pull the data out of the HTML into objects first, then pipe the objects to ConvertTo-Json. For a clean table, the quickest way is to cast the HTML to [xml]: Real web pages are rarely clean enough for that. Below I cover the [xml] route, why it breaks … Read more

50 PowerShell Interview Questions and Answers (2026)

PowerShell interview questions and answers with tested examples

Most PowerShell interview questions come back to one idea: PowerShell passes .NET objects, not text. If you can show that in two lines, you’ve answered half the screening round already: Output: Get-Item returned a FileInfo object, so the file size is a real number you can sort, compare and add up. A text-based shell would … Read more

How to Generate Random Numbers in PowerShell (7 and 5.1)

Generate random numbers in PowerShell with Get-Random and .NET

Need a random number between 1 and 10? To generate random numbers in PowerShell, call Get-Random with a range, and remember the maximum is never returned: That prints a whole number from 1 to 10. Below, I cover decimals, several numbers at once, seeds, 6-digit codes, .NET options and secure numbers, each tested in PowerShell … Read more

PowerShell Pipeline Variables: $_, $PSItem and -PipelineVariable

PowerShell pipeline variables with $_, $PSItem and -PipelineVariable

PowerShell pipeline variables come in two kinds: the automatic $_ (also called $PSItem), which holds the current object, and the -PipelineVariable common parameter, which saves an object under your own name for later pipeline steps: Output: Here $order still points at the CSV row two commands later, while $PSItem is whatever reached the current command. … Read more

How to Get the Computer Name in PowerShell (7 and 5.1)

Get computer name in PowerShell with five methods compared

The fastest way to get the computer name in PowerShell is the COMPUTERNAME environment variable. Type this and press Enter: It works the same in PowerShell 7 and Windows PowerShell 5.1 on Windows. The other methods below return almost the same thing, but they differ in letter case, in remote support, and on Linux or … Read more

How to Convert a Relative Path to an Absolute Path in PowerShell

Convert a relative path to an absolute path in PowerShell

To convert a relative path to an absolute path in PowerShell, use Resolve-Path. It resolves the path against your current PowerShell location and returns the full path: Resolve-Path only works for paths that exist, though, and the popular .NET shortcut can quietly use the wrong folder. Below I cover both problems, plus paths relative to … Read more

How to Count Occurrences of a Substring in a String in PowerShell

Count occurrences of a substring in a string in PowerShell

To count occurrences of a substring in a string in PowerShell, escape the substring and count the regex matches. This one line returns how many times $find appears in $text: The count you get depends on case, special characters and overlaps, and a few popular shortcuts quietly get it wrong. I ran each method below … Read more

How to Convert an Object to a String in PowerShell

Convert an object to a string in PowerShell

To convert an object to a string in PowerShell, pipe it to Out-String, which returns the same text you’d see in the console. Add .Trim() to drop the blank lines around it: That works for custom objects, cmdlet output and hashtables. When you want one value in a sentence, you’ll use property expansion, JSON or … Read more