The IANA (Internet Assigned Numbers Authority) maintains a CSV file with all known port assignments. PowerShell can download this list for you: $out...
ps1
World Time Clock
PowerShell 5 comes with Get-TimeZone which returns all defined time zones and their time offset. This is all you need for a one-liner world clock:...
Check for Daylight Savings Time
Here is how PowerShell can find out whether Daylight Savings Time is currently effective – a potentially needed detail when doing GMT...
Setting Time Zone
While you need administrative privileges to adjust time and date on your computer, each user can change the time zone, i.e. when you travel....
Finding All Variables in a Script
In the previous tip we illustrated how you can analyze the content of a script block and search for variables or commands. The same is possible for...
Finding All Variables in a Script Block
To analyze the content of a script block, you can easily examine the AST, and, for example, create a list of all variables in the code: $code = { $a...
Running PowerShell Script as a Scheduled Task
If you need to run a PowerShell script in regular intervals, why not run it as a scheduled task? Here are some lines that help you create a new...
Spying on Function Source Code
Here is a quick way how to find the source code of PowerShell functions: ${function:Clear-Host} | clip This would copy the Clear-Host source code to...
Modern Alternative to More
In a PowerShell console, you can continue to pipe to more, just like in cmd.exe, to view results page by page. However, more does not support...
Creating SMB Shares Remotely
Here are a couple of lines that remotely create an SMB share on a server: #requires -Version 3.0 -Modules CimCmdlets, SmbShare -RunAsAdministrator...
Important PowerShell Variables
Here is a list of important PowerShell variables: $pshome is the path to the place where PowerShell lives. $home is the path to your personal...
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...