Controlling Numeric Range for Variables

In a previous tip we illustrated how in PowerShell 3.0, you can now use validators to restrict the data that can be assigned to a variable. Here is another example. It assigns the data type "Int32" to a variable, so it only accepts numeric values. In...

Killing All Remote PowerShell Sessions

In a previous tip we explained how you can enumerate who is connected to your machine via PowerShell Remoting, and how you can terminate individual remote shells. If you want to simply kill all remote PowerShell sessions running on your machine, you can restart the...

Killing Remote PowerShell Sessions

To kill a remote PowerShell session that runs on your machine, you could kill the wsmprovhost.exe task associated with it, but identifying the correct process and killing it is not a robust and recommended way. A better way uses this approach: $URI =...

Finding Remote Sessions Connected to Your Machine

Ever wanted to find out who is running a (hidden) remote PowerShell on your machine? Here's a simple one-liner: Get-WSManInstance -ConnectionURI ('http://{0}:5985/wsman' -f $env:computername) `-ResourceURI shell -Enumerate It will return anyone connecting...

Define Variable Checkers

When you assign a mandatory data type to a variable, then this will improve script robustness: whenever a value is (accidentally) assigned to that variable that does not match the expected type, an error is raised. In PowerShell 3.0, this concept can be further...

Enabling Visual Styles

When you use Windows Forms dialogs and windows, you may have noticed that they show differently, depending on whether you launched your script from within the ISE editor or from the powershell.exe console. This is because the ISE editor enables visual styles by...

Finding PowerShell Module Requirements

PowerShell modules extend functionality by adding more cmdlets and/or providers. Modules support copy-and-paste deployment ("XCopy-deployment", if you like), so theoretically, you can copy any module from one computer to another and use it there (use...

Using ValidatePattern attribute

PowerShell can validate parameters using regular expressions. This example accepts only computer names that start with "PC" followed by three digits: function Get-PCInfo { param ( [ValidatePattern( '^PC\d{3}$' )] [String] $ComputerName )...

Creating Script Blocks from Strings

Sometimes you may want to create a script block dynamically from a string. This could be necessary to include local variable content, for example. To turn a string into a script block, you cannot cast the string. Instead, use this approach: $folder =...

Analyzing Script Blocks

Script blocks are simply pieces of PowerShell code. You probably use them all the time in scripts and functions. A script block is defined by braces and could be as simple as this: $a = { Get-ChildItem C:\ } Now, the variable $a holds code that can be handed over to...

Creating Simple Custom Dialog

PowerShell can display custom dialogs easily. So if you're not satisfied with the dialog buttons available in a standard MsgBox dialog, simply create your own. Here's the raw code for a very simplistic two-button dialog that appears centered on screen above...

Getting Screen Information

Ever needed to know the current screen resolution or related screen information? Here's an easy way of getting that information: Add-Type -AssemblyName System.Windows.Forms [System.Windows.Forms.Screen]::AllScreens [System.Windows.Forms.Screen]::PrimaryScreen Note...

Disassembling C# Code

Adam Driscoll, another PowerShell MVP, has published a function called Get-MemberBody to PoshCode: http://poshcode.org/4127. In conjunction with the free tool "ILSpy", you now can easily disassemble and look at source codes for .NET functions. After you...

Understanding Profile Scripts

Whenever you want PowerShell to configure or execute code automatically on launch, the code needs to go into one of four profile scripts. They really work pretty much like autoexec.bat for Windows in old days, and execute whenever PowerShell starts. You can find the...

Test Local User Account Credentials

Here is a snippet that verifies a local user account. Simply submit a username and a password. You get back either $true or $false: $username = 'Administrator' $password = 'P@ssw0rd' $computer = $env:COMPUTERNAME Add-Type -AssemblyName...

Enabling High Performance Power Plan via Command Line

Enabling High Performance Power Plan via Command Line

Hopefully, everyone knows by now that the default power plan in Windows 2008+ is “balanced” which can result in your CPUs being throttled down to 67% or even 50% power. This would be equivalent to running at full power with 2/3 or 1/2 as many CPUs. Make no mistake...