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

Power Tips

Power Tips

Syndication

  • Aliases Can Be Dangerous

    All PowerShell versions Aliases enjoy the highest priority among executable commands in PowerShell, so if you have ambiguous commands, PowerShell always picks the alias. This can be dangerous: if you allow others to change your PowerShell environment...
  • Converting Special Characters, Part 2

    All PowerShell versions In a previous tip we illustrated how you can replace special characters in a text. Here is another approach that may be a bit slower but is easier to maintain. It also features a case-sensitive hash table: function ConvertTo -...
  • Converting Special Characters, Part 1

    All PowerShell versions Sometimes it becomes necessary to replace special characters with other characters. Here is a simple function that does the trick: function ConvertTo - PrettyText ( $Text ) { $Text . Replace ( ' ü ' , ' ue '...
  • Test-Driving Scripts without Aliases

    All PowerShell versions Aliases can be cool in interactive PowerShell but should not be used in scripts. In scripts, use the underlying commands (so use “Get-ChildItem” instead of “dir” or “ls”). To test drive a script...
  • Delete Aliases

    All PowerShell versions While you can easily create new aliases with New-Alias or Set-Alias, there is no cmdlet to delete aliases. PS> Set-Alias -Name devicemanager -Value devmgmt.msc PS> devicemanager PS> To delete an alias, you would typically...
  • Finding AD Accounts Easily

    All PowerShell versions You do not necessarily need additional cmdlets to search for user accounts or computers in your Active Directory. Provided you are logged on to the domain, simply use this: $ldap = ' (&(objectClass=computer)(samAccountName...
  • Loading Functions from Separate File

    PowerShell 3.0 and newer To keep things simple, you may want to put PowerShell functions into a separate file. To load these functions into your job scripts, you can use a simple approach. Make sure the script file with your PowerShell functions is stored...
  • Creating Great Reports

    All PowerShell versions You can change all properties of objects when you clone them. Cloning objects can be done to “detach” the object data from the underlying real object and is a great idea. Once you cloned objects, you can do whatever...
  • Accepting Multiple Input

    All PowerShell versions When you create PowerShell functions, here is a template that defines a InputObject parameter that will accept multiple values both via parameter and via pipeline: function Get-Something { param ( [ Parameter ( Mandatory = $true...
  • Reading Registry Values Easily

    All PowerShell versions Here is the simplest way to read Registry values: $Key = ' HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion ' $Name = ' RegisteredOwner ' $result = ( Get-ItemProperty -Path "Registry::$Key"...
  • Copying Command History as a Tool

    In a previous tip we illustrated how you can copy the previously entered interactive PowerShell commands to your favorite script editor. Here is a function that makes this even easier. If you like it, you may want to put it into your profile script so...
  • Copying Command History

    If you played with PowerShell and suddenly notice that some of the lines of code you just entered actually work, then you may want to copy and paste them into a script editor, save them, or show them to friends. Here is how: Get-History -Count 5 | Select...
  • Finding Dates Between Two Dates

    If you must know how many days are between two dates, you can easily find out by using New-TimeSpan: $startdate = Get-Date $enddate = Get-Date -Date ' 2014-09-12 ' $difference = New-TimeSpan -Start $startdate -End $enddate $difference . Days However...
  • Using Default Parameters

    In PowerShell 3.0, an option was added to define default values for arbitrary cmdlet parameters. This line, for example, would set the default value for the parameter -Path of all cmdlets to a given path: $PSDefaultParameterValues . Add ( ' *:Path...
  • Speeding Up Scripts with StringBuilder

    Often, scripts add new text to existing text. Here is a piece of code that may look familiar to you: Measure-Command { $text = "Hello" for ( $x = 0 ; $x -lt 100000 ; $x ++ ) { $text += "status $x" } $text } This code is particularly...
1 2 3 4 5 Next > ... Last »
Copyright 2012 PowerShell.com. All rights reserved.