powertips

Adding New Lines to Strings

In a previous tip you learned that text arrays can easily be multiplied. The same is true for assignment operators such as +=. When you apply this...

Creating Multiline Strings

You probably know what this line produces: 'Hello' * 12 Right, you get 12 times the string you submitted. If you wanted new lines instead, a...

Enumerating Registry Keys

To enumerate all subkeys in a Registry key, you might be using a line like this: PS> Dir...

Asking for Credentials

When you write functions that accept credentials as parameters, add a transformation attribute! This way, the user can either submit a credential...

How to List Registry Hives

Use the provider name instead of a drive name when you need to get a list of all registry hives: Dir Registry:: ReTweet this Tip!

Writing Registry Key Default Values

If you need to set the default value for a registry key, you can use either of these approaches: Set-ItemProperty -Path HKCU:\Software\Somekey -Name...

Use Dumping Help

You can start by dumping all Help information into a file to learn more about a PowerShell cmdlet. You can then read all details about the cmdlet...

Use Out-GridView Requirements

Out-GridView is a great way to present results in a “mini-Excel” sheet: Get-Process | Out-GridView However, Out-GridView has two...

Use the CTRL+Arrow

Inside the PowerShell console, you can hold CTRL while pressing the arrow key to move the cursor word-by-word. This way, you can move the cursor...

Change Service Startmode

You can use WMI like this if you want to change a service start mode: ([wmi]'Win32_Service.Name="Spooler"').ChangeStartMode('Automatic').ReturnValue...

Determining Service Start Modes

By using WMI, you can enumerate the start mode that you want your services to use. To get a list of all services, try this: Get-WMIObject...

Print All PDF Files in Folders

Try this one-liner if you need to print out all PDF documents you have stored in one folder: Dir c:\myfolder\*.pdf | Foreach-Object { Start-Process...

Store Pictures in Active Directory

When you need to store a picture into an AD account, the picture will have to be converted to byte values before it can be stored. Just make sure...

Finding IP and MAC address

When you query network adapters with WMI, it is not easy to find the active network card. To find the network card(s) that are currently connected...

Turning SIDs into Real Names

Sometimes, you'd like to turn security identifiers (SIDs) into real names. Here is a function that can do this for you: function SID2Name($sid){...

Converting User Names to SIDs

If you want to translate a valid user name to its security identifier (SID), here is a function to do that for you: function Name2SID($name,...

Changing Units

When you list folder contents, file sizes are in bytes. If you'd rather like to view them in MB or GB, you can use calculated properties, but by...

Closing Excel Gracefully

When you access Microsoft Excel from script, you may have noticed that it never gets removed from memory again, even if you call its Quit() method:...

Formatting Currencies

Formatting numbers as currencies is straight-forward - as long as it is your own currency format: '{0:C}' -f 12.22 If you want to output...

Using Regular Expressions with Dir

When you use Dir (alias: Get-ChildItem) to list folder contents, you can use simple wildcards but they do not give you much control. A much more...

Creating PowerShell Menus

PowerShell is console based, so its default menus are console based as well. To offer choices to your users, here is some sample code to create a...

Scanning Registry for ClassIDs

The Windows Registry is a repository filled with various Windows settings. Get-ItemProperty can read Registry values and accepts wildcards. So, with...

Bypassing Execution Policy

When execution policy prevents execution of PowerShell scripts, you can still execute them. There is a secret parameter called "-" . When you use...

1 70 71 72 73 74 98