June 2009 - 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
  • Assigning Values to Parameters

    Cmdlets and Functions most of the time support one or more parameters. Let's take this simple function. It defines two parameters called number and force. function test([ Int ] $number = 0, [ switch ] $force ) { "You specified: number=$number...
  • Avoid Format-... in Scripts

    The family of Format-... Cmdlets is useful for outputting data into the console. You probably often used lines like this: Get-Service | Format-Table Name Be aware that Format-... Cmdlets change the objects and return a formatting object used for displaying...
  • 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...
  • Conflicting Commands

    PowerShell supports many different command categories and searches for the command in the following order: 1. Alias 2. Function 3. Cmdlet 4. Executable 5. Script 6. Associated Files This enables you to override existing commands. For example, if you define...
  • Extending Alias Functionality

    Alias names are a good way of making commands more accessible. The following line would enable you to quickly launch Internet Explorer by entering the IE alias: Set-Alias IE "$env:programfiles\Internet Explorer\iexplore.exe" Aliases only replace...
  • Finding Alias Names

    To find out all alias names associated with a given command, filter the alias list by its definition property. The following command lists all aliases that point to the Get-ChildItem cmdlet: Get-Alias | Where-Object { $_ . Definition -eq ' Get-ChildItem'...
    Filed under:
  • Trustworthy Folders

    If you want to launch a script file or executable, in PowerShell, you'll need to specify either a relative or absolute pathname. To do so by name, you’ll need to make sure the folder is trustworthy, which could be done simply by adding it to...
  • Removing Illegal File Name Characters

    If you have to batch-process tons of file names, you may want to make sure all file names are legal and automatically remove all illegal characters. Here is how you can do that: $file = "this*file\\is_illegal<>.txt" $file $pattern = "...
  • Removing Illegal Path Characters

    You can always strip all illegal characters from the path If you have no time to review path names and correct them manually to see if they contained illegal characters. Here is how you can do that: $path = "c:\<>illegal path" $path $pattern...
  • Using Test-Path to Validate A Path

    While raw .NET calls provides you with granular control over how to validate paths and file names, there is a cmdlet called Test-Path for simple purposes. Its primary purpose is to validate whether a file or path exists. When adding the -isValid parameter...
  • Checking File Names for Invalid Characters

    File names may not contain certain characters because they are illegal and cannot be processed by Windows. First, let's find out which characters are considered to be illegal in a file name. Use the System.IO.Path .NET class and its GetInvalidFileNameChars...
  • Checking Paths for Invalid Path Characters

    Paths may not contain certain characters because they are illegal and cannot be processed by Windows. First, let's find out which characters are considered to be illegal in a path. To get that list, use the System.IO.Path .NET class and its GetInvalidPathChars...
  • Using Advanced Path Functions

    Whenever the built-in Split-Path cmdlet isn't enough, you can always resort to the real .NET class behind it. This class is called System.IO.Path, Use Get-Member with the -static parameter to list its members: [ System.IO.Path ] | Get-Member -static...
  • Using Simple Path Functions

    PowerShell comes with the Split-Path cmdlet, which helps you disassemble paths and find the interesting information. Take a look: Split-Path c:\test\ file.txt Split-Path c:\test\ file.txt -isAbsolute Split-Path c:\test\ file.txt -leaf Split-Path c:\test...
  • Using VB.NET to Migrate From VBScript

    If you have ever written scripts using VBScript and now are trying to migrate them to PowerShell, you may miss a lot of useful commands not available in PowerShell. However, you can continue to use your favorites such as MsgBox() or InputBox()with a little...
1 2 Next >
Copyright 2012 PowerShell.com. All rights reserved.