PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Forum experts available to answer your PowerShell questions

Power Tips

Power Tips

Syndication

  • Using String Functions

    PowerShell uses .NET objects everywhere. Anything is represented as .NET object, and .NET objects come with useful built-in methods. However, for string manipulation you do not need to look for sophisticated external commands as they are built right into...
  • Returning Exit Code from Script

    When running a PowerShell script, you may want to return a numeric exit code to the caller to indicate failure or success. You should use the "exit" statement to return whatever numeric exit code you want. The following line in a script would...
  • Multidimensional Arrays

    PowerShell supports two types of multi-dimensional arrays: jagged arrays and true multidimensional arrays. Jagged arrays are normal PowerShell arrays that store arrays as elements. This is very cost-effective storage because dimensions can be of different...
  • Trap and Try/Catch

    Trap, which has been around since PowerShell v.1, is designed to catch errors and works like this: trap { Write-Host -foregroundcolor Yellow ` "Something terrible happened: $($_.Exception.Message)" ; ` continue } & { dir nonexistent: -ErrorAction...
  • Which PowerShell Version Am I Running

    As PowerShell v.2 becomes more common , you may want to check which PowerShell version a machine is running. Use this to differentiate between v.1 and higher versions from within PowerShell: $isV2 = test-path variable:\psversiontable Check this registry...
  • Checking File and Folder Permissions

    Get-Acl is a convenient Cmdlet to expose NTFS file and folder settings. For example, to get a list of ownerships for a folder content, do this: Dir | Get-Acl To find out which "Identities" (Users or groups) have specific permissions granted...
  • Using Switch Parameters

    Switch parameters work like a switch, so they are either "on" or "off" aka $true or $false. To add a switch parameter to a function, cast the parameter to [switch] like this: function test([ switch ] $force ) { $force } When you call...
  • Sorting Arrays

    Let's assume you have an array of items which you would like to sort. Here is the PowerShell way: $array = 1,5,32,5,7 $array | Sort-Object $array = "Hello" , "World" , "Test" , "A" , "Z" $array | Sort...
  • Finding the Current User

    Should you try and use PowerShell as a log-on script, you may want to know who is actually running the script to access user specific folders or settings. The .NET Environment class can tell you who is running a script. [ Environment ]:: UserDomainName...
  • Reading Registry Values

    In PowerShell, you will need to use one of the Registry virtual drives to read from the Windows Registry as it is - not always intuitive. Here are two functions to make your life easier. Get-RegistryValues lists all values stored in a registry key: function...
  • Calling VBScript From PowerShell

    Sometimes, you may have an existing VBScript that already does just what you want. You can easily incorporate any VBScript into PowerShell because PowerShell can call just about anything that is executable, including VBScript. The tricky part is that...
  • Finding Current Script Path

    Ever wanted to locate the path of your current PowerShell script? This can be useful to call other scripts or resources in the same folder. To determine the folder your current PowerShell script is located in, try this: Split-Path -parent $MyInvocation...
  • Encrypting PowerShell Scripts

    Sometimes, you may want to hide the code of your PowerShell script in order to protect passwords contained within the code. One way to safely encrypt PowerShell script is by converting it into a secure string. You must first create a sample script you...
  • Converting ASCII and Characters

    To convert the ASCII value to a character, use type casting like this: [ char ]65 To do the opposite and convert a character to its ascii value, use this: [ int ][ char ] 'A' Here, the letter "A" is first converted into a Char type,...
  • Outputting HTML Reports

    PowerShell can export results as HTML. Simply pipe the results to ConvertTo-HTML and save the result in a file. When you do that, it is wise to use Select-Object to first limit the object properties to only those you want to see in your report, otherwise...
1 2 3 4 5 Next > ... Last »
Copyright 2012 PowerShell.com. All rights reserved.