December 2008 - Power Tips

Share |

Join PowerShell.com!
Subscribe to Feed

PowerShell eBook
Sign up for
Your PowerTip of the Day:

Award-winning PowerShellPlus


Admin Guide to PowerShell Remoting


Mastering PowerShell eBook

Welcome to the archive of tips delivered through Tobias' Tip of the Day RSS Feed and Your Power Tip of the Day email. Subscribe in the sidebar to get the latest tips!

Sort by: Most Recent | Most Viewed | Most Commented
  • Converting User Input to Date

    PowerShell uses the US/English date format when converting user input to DateTime, which can cause unexpected results if using a different culture. For example, on German systems "1.3.2000" resolves to March 1, 2000. PowerShell can convert this...
  • Casting a Type Without Exception

    Read-Host is a useful cmdlet to use to ask for user input. However, it returns user input always as generic string. Of course, you can always convert the user input to a more specialized type, like DateTime, to calculate time spans: $date = [ DateTime...
  • Order Matters

    Here is a challenge for you. The following code is a simple currency converter. However, when you run it, you'll notice it doesn't convert correctly. Instead, you always get back the result you entered: $number = Read-Host 'Enter amount in...
  • Filtering Based On File Age

    Every so often, you'll need to filter files by age. Maybe you'll only want to see files that are older than 20 days old and delete them or back them up. So, you need a way to filter file age relative to the current date. Here is a custom filter...
  • Accessing Date Methods

    While Get-Date returns the current date and time, it really returns a DateTime object. You can use this object to find out more about the date or to calculate date and time offsets as it has a number of very useful properties and methods. $date = Get...
  • Using Cultures

    Since PowerShell is culture-independent, you can pick any culture you want and use the culture-specific formats. The following script instantiates the Japanese culture, outputting a number as currency first in your current culture and then in the Japanese...
  • Outputting Nicely Formatted Dates

    Get-Date provides you with the current date and time. With the -format parameter, you can add style to it. For example, use -format with a lowercase d to just output a short date: Get-Date -Format d You can get a list of format characters directly at...
  • Stopping and Disabling Services

    You may find that Vista's new Instant Search can sometimes get out of hand and slow down your machine. Temporarily disabling and then stopping the search service is one way to deal with this issue: Set-Service wsearch -startupType Disabled Stop-Service...
  • Finding Cmdlets With a Given Parameter

    Finding cmdlets by name is easy: Get-Command * service * -commandType Cmdlet But how can you list all cmdlets that support a given parameter? If you'd like to see all cmdlets with a -List parameter? The easiest way is to use Get-Help with the parameter...
  • Accessing Static .NET

    You can start to explore the power of .NET with PowerShell's built-in .NET access.. All you will need are square brackets to access static classes. For example, here is a code snippet that resolves a host name: [ system.net.Dns ]:: GetHostByName ...
  • Arrays of Strings

    In PowerShell, you can multiply strings: the string is repeated which can be useful for creating separators: '-' * 50 This works for words, too: 'localhost' * 10 You can create a text array by converting the text to an array by first wrapping...
  • Quick Loops

    Normally, creating a simple loop in PowerShell can be a bit awkward: for ( $x = 1; $x -le 10; $x ++ ) { $x } A much more readable way works like this (and uses an array internally): foreach ( $x in 1..10) { $x } Loops are fun as you can easily create...
  • Understanding Exceptions (and why you can't catch some errors)

    Traps are a great way of catching exceptions and handling errors manually but this does not seem to work all of the time. This example catches the error: Trap { 'Something terrible happened.' ; Continue } 1 / $null However, this example does not...
  • Understanding Trap Scope

    Traps are a great way of handling errors but you may want to control where PowerShell continues once an error occurs. There is a simple rule: a trap that uses the Continue keyword continues execution in the next line in the scope of the current trap....
  • Using Traps and Error Handling

    Traps are exception handlers that help you catch errors and handle them according to your needs. A Trap statement anywhere in your script: Trap { 'Something awful happened' } 1 / $null Whenever an error occurs and an exception is raised, PowerShell...
1 2 Next >
Copyright 2012 PowerShell.com. All rights reserved.