All PowerShell versions When you run native EXE console commands inside your scripts, these commands typically return a numeric return value. This...
ps1
Compacting Paths
[WindowsAPILib.Helper _i="0" _address="0" theme_builder_area="post_content" /][WindowsAPILib.Helper _i="1" _address="1"...
Get the OU from an LDAP Path
All Versions To extract certain parts from raw strings, you can often use a combination of text splitting and text substring commands. For example,...
Creating Huge Dummy Files
All PowerShell versions If you need to stress test systems, or need large dummy files for other purposes, here is some code that can create even...
Limiting Maximum String Length
All Versions To make sure output text is not excessively long, you can use logic like this to shorten text that is longer than a given maximum...
Setting Permissions in AD or Windows Registry
ActiveDirectory Module We already illustrated previously how you can use Get/Set-Acl to read and write permissions to files and folders. The truth...
Catching Errors in Native EXEs (Part 2)
All versions Here is yet another approach that you can use to detect errors emitted by console applications: $ErrorActionPreference = 'Continue'...
Replacing NTFS Permissions with SDDL Information
All PowerShell versions With Get-Acl, you can output the security information from files and folders as plain text in SDDL format (Security...
Cloning NTFS Permissions
All PowerShell Versions Here is some simple code that reads the NTFS permissions from one folder and applies the exact same settings to another....
Catching Errors in Native EXEs (Part 1)
All Versions When you run native console EXE commands such as robocopy.exe, ipconfig.exe, or similar commands, you can handle errors raised by these...
Using WMI Inheritance
All Versions WMI classes are inherited from each other, and that’s something you can take advantage of. Take this line: PS> Get-WmiObject...
Finding Process Owners
All PowerShell versions To find out who owns a particular process and how many instances are running, try this simple piece of code: $ProcessName =...
Parsing DISM Log File
PowerShell 2.0 and later In your Windows folder, you find all kinds of system log files. One is DISM.log which contains information about the...
Setting Active Directory Attributes
ActiveDirectory Module To set AD attributes for a user account, PowerShell uses hash tables. That’s a versatile approach that lets you specify...
Bulk Creating AD Users from Excel Sheets
Module ActiveDirectory To create a large number of new Active Directory users, you can import the user data from a CSV file, for example export an...
Removing AD Group Members
Module ActiveDirectory To remove one or many users from an Active Directory group, try this approach: $user = @() $user += Get-ADUser -Filter { Name...
Steps to Configure PowerShell (Part 3)
All PowerShell versions If you use PowerShell at home or in an environment without central group policy management, here are some additional steps...
Steps to Configure PowerShell (Part 2)
PowerShell 2.0 and later If you use PowerShell at home or in an unmanaged environment, here are some additional steps you should consider to make...
Steps to Configure PowerShell (Part 1)
PowerShell 2.0 and later If you use PowerShell at home or in an unmanaged environment, here are some steps you should consider to make PowerShell...
NULL Values in Arrays
All PowerShell versions Whenever you assign NULL values to array elements, they will count as array elements, but will not be output (after all,...
Randomize Lists of Numbers
All PowerShell versions This line will take a list of numbers and randomize their order: Get-Random -InputObject 1, 2, 3, 5, 8, 13 -Count...
Reading Disks and Partitions
Windows 8.1 / Server 2012 R2 Disk management has been greatly simplified with the many new client and server cmdlets that ship with Windows 8.1 and...
Finding Out Windows Version
All PowerShell Versions Do you own Windows 8.1 Basic, Pro, or Enterprise? Finding out the Windows version is easy. Finding out the exact subtype is...
Join-Path Fails with Nonexistent Drives
All Versions To construct path names from parent folders and files, you may have been using Join-Path. This cmdlet takes care of the correct number...