The "+=" operator is pretty convenient and can add new elements to an array. If you need this more than once, for example in a loop, then...
Powershell
Copy Command History to a Script
Sometimes you may have played around with the interactive PowerShell and suddenly realized that some of the commands you played with were pretty...
Compare Versions
Ever wanted to compare software versions? If you do it like this, the result is off: PS> '3.12.11.100' -gt '11.1.22.91' True...
Cleverly Aborting Endless Loop
In a previous tip you have seen how an endless loop can be used to continuously monitor things - until PowerShell is closed, or a user presses...
Finding Executable Path
Here is a one liner telling you the exact location of the executable of any running process. The example returns the path to the PowerShell...
Doing Things Forever
If you want PowerShell to run forever, for example in order to continuously ping a site, use a simple endless loop: #requires -Version 2...
Switching Keyboard Layout with PowerShell
Next time you find yourself with a PowerShell console that uses the wrong keyboard layout, keep your fingers off the mouse! Do it with PowerShell...
Getting Basic Networking Information
Beginning with Windows 8.1 and Server 2012 R2, the operating system adds a wealth of new cmdlets for OS management. With these cmdlets, it is almost...
Investigating AD Classes
Active Directory organizes its content in classes like "user" or "computer". Each class has a predefined set of attributes, like...
Use Ctrl+Space in ISE!
The PowerShell ISE opens IntelliSense menus frequenty and helps you write code. Sometimes, however, IntelliSense does not pop up automatically,...
PowerShell Killing Itself
If you schedule a script as a scheduled task, or call it externally, and want to make sure the PowerShell process really ends, here is a brute force...
Converting Arrays to Strings in CSV Exports
When you export objects to CSV--for example to display them in Excel--arrays won't be output correctly. Here is a simple way that converts...
Exploring PowerShell Automatic Variables
Here is an easy way to get a list of all currently defined variables, their values, and their purpose. #requires -Version 2 Get-Variable |...
Invoking Code Repeatedly
Sometimes you might want to run some command multiple times until it runs successfully. Here is a function that shows a way to do this: #requires...
Saving Persistent Data
Sometimes a script needs to save information in a persistent way. Maybe you have a list of computers that you'd want to contact, but only some...
Use WMI the Modern Way!
WMI is a powerful technique to find out information about local or remote computers, and you may have used Get-WmiObject before to do so (if not,...
Using .NET Types Directly
Cmdlets contain pure .NET code, so thanks to cmdlets, you do not need to directly touch .NET code. You can, however. Here are a number of sample...
Using Workflows to Parallelize Code
If you want to execute more than one thing at once, there are many ways to implement this in PowerShell. One may be the use of workflows. They were...
Decorate Scripts with #requires Statements
PowerShell supports a number of #requires statements. Technically they are comments but PowerShell checks the requirements, and if they are not met,...
Do Not Mix Different Objects!
If you do output completely different objects, you may lose information. Take a look at this example: #requires -Version 2 $hash = @{ Name =...
Getting an Excuse
Here is a quick way of getting a good excuse - provided you have Internet access: #requires -Version 3 function Get-Excuse { $url =...
Who is Listening? (Part 2)
If you run at least Windows 8 or Windows Server 2012, you can use Get-NetTcpConnection to find out which network ports are in use, and who is...
Who Is Listening? (Part 1)
The good oldfashioned netstat.exe can tell you the ports that applications listen on. The result is plain-text, though. PowerShell can use regular...
Sending Objects to Notepad
In a previous tip we showed how you can send text to a fresh Notepad instance. Today, you get an enhanced version of Out-Notepad: you can pipe...