Variables and function parameters can be automatically validated through validation attributes. Here is a simple example making sure $test1 can only...
Powershell
Cloning DHCP Server Settings
Beginning with Windows Server 2012, you can easily export and re-import DHCP settings. Cloning or migrating DHCP servers is a snap. The example...
Searching for ADUsers
The free Microsoft RSAT tools come with the PowerShell “ActiveDirectory” module: plenty of cmdlets help you administer Active Directory...
ToString() Masquerade
In the previous tip we explained that ToString() is a fuzzy way of describing an object, and that the object author can decide what ToString()...
Careful with ToString()
Any .NET object has a method ToString() that returns a text representation. This is also what you get when you output an object in a string....
Validating Integer Variables
You can easily assign the [Int] type to a variable to make sure it can contain only digits. But did you know that you can also apply a regex...
Creating Random MAC Addresses
If you just need a bunch of randomly generated MAC addresses, and you don’t care much about whether these addresses are actually valid, then...
Bitwise Shift
PowerShell contains some binary operators that are not so commonly used, for example bitwise shifting. The -shl operator shifts bits to the left:...
Converting Binary String to Integer
Here is how you convert a binary text string into the corresponding integer value: $binary = "110110110" $int =...
Using Clipboard to Transfer Data and Results
Finally in PowerShell 5, there is support for sending results to the clipboard, and receiving results from the clipboard: PS> Get-Command -Noun...
Installing MSI on Remote System
How .Replace() and -replace differ
There are two ways of replacing text in a string: the Replace() method, and the –replace operator. They work fundamentally different....
HTML Encoding Advanced
The static .NET method HtmlEncode does a good job encoding the usual character codes but fails with many special characters. To encode all...
HTML Encoding
There is a static .NET method that you can use to HTML-encode text, for example if you want the text to display correctly in HTML output: PS>...
Bulk Printing Word Documents
This line finds all Word documents in your profile: Get-ChildItem -Path $home -Filter *.doc* -Recurse If you’d like, you can easily print them all....
Downloading Videos From German Media Databases
In Germany, there are publicly available media databases with most of the TV content broadcasted by public stations. It just takes very little...
Translating Error Records
Whenever PowerShell records an error, it wraps it in an Error Record object. Here is a function that takes such an error record and extracts the...
Enable AD Users with Out-GridView
Sometimes it requires just a couple of lines of code in PowerShell to produce highly useful helpdesk tools. Here is one that displays all currently...
Turning AD User into a Hash Table
Sometimes it could be useful to load all attributes from a given AD user into a hash table. This way, you could edit them, and then use Set-ADUser...
Cloning Folder Structures (with NTFS Permissions) – Part 2
In the previous tip we illustrated how Get-FolderStructureWithPermission can document and create a list of all nested folders in a structure, along...
Cloning Folder Structures (with NTFS Permissions) – Part 1
Sometimes you need to re-create a nested folder structure, and may also want to clone the NTFS permissions. Today we are tackling the first step:...
Evaluating Exit Codes (aka Error Level – Part 3)
In part 3 of our mini-series about running console applications in PowerShell, here is a goodie: how can you run a console application separately...
Combine PowerShell and SQL Diagnostic Manager to Automate SQL Server Monitoring
Run new and existing PowerShell scripts with SQL Diagnostic Manager and utilize the vast power of PowerShell via the customizable monitoring...
Evaluating Exit Codes (aka Error Level – Part 2)
When you directly launch a console-based application, PowerShell returns its exit code (aka Error Level) in the automatic variable $LASTEXITCODE....