Powershell

Read-Host Blocks Automation

Using Read-Host to ask for user information can be problematic because it prevents scripts from running unattended. A better way could be to wrap...

Force Client Time Resync

If your client does not sync time correctly with your domain controller, try the code below. It does require Admin privileges: w32tm.exe /resync...

Mapping Network Drives

PowerShell offers numerous ways to connect to SMB file shares. Here are three different approaches: # adjust path to point to your file share...

Safely Deleting Data

To safely delete files, folders, or entire drives, PowerShell can use the built-in cipher.exe tool. This line would safely delete the old user...

Finding Organizational Units

Get-OrganizationalUnit (from Microsofts free RSAT tools) can search for organizational units based on fully distinguished name or GUID, or you can...

Testing Organizational Unit

Provided you have installed the free Microsoft RSAT tools, here is a simple way to check whether an OU exists: $OUPath =...

Validating Variables

Variables and function parameters can be automatically validated through validation attributes. Here is a simple example making sure $test1 can only...

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

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

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

1 34 35 36 37 38 104