August 2011 - 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
  • Validating Function Parameters

    You can use Regular Expression patterns to validate function parameters: function Get-ZIPCode { param ( [ ValidatePattern ( ' ^\d{5}$ ' )] [ String ] $ZIP ) "Here is the ZIP code you entered: $ZIP" } You can add a [ValidatePattern()...
  • Re-Encoding ISE-Scripts in UTF8

    PowerShell ISE by default saves .PS1 scripts in a highly unusual big endian Unicode encoding. For example, you cannot digitally sign such files.. Here is a one-liner that will read in all PS1 scripts in your current folder and saves them using UTF8 encoding...
  • Solving Problems with Parenthesis

    You use parenthesis in PowerShell to control an execution order. However, some language keywords are not legal inside parenthesis, like try and catch. See what happens with these two lines: try { 1 } catch { 2 } ( try { 1 } catch { 2 }) The second line...
  • Removing CSV Headers

    Try this to remove column headers from a CSV file: $result = get-process | ConvertTo-Csv -Delimiter ";" $count = $result . Count - 1 $result [ 2 .. $count ] | Out-File $home\test.csv notepad $home\test.csv You can also use this to append raw...
  • Check Whether a Program is Running

    If you'd like to find out whether an instance of WinWord (or any other program) is currently running, you can try this: ( Get-Process winword -ea 0 ) -ne $null This line will returns $true if at least one instance is running. Note that this will also...
  • Finding Files

    Dir (Get-Childitem) is a simple, but effective way to find files. The following line will find any file or folder called "hosts" anywhere inside your Windows folder. It may take some time for the results to display because this command will...
  • Opening Results in Excel

    To send PowerShell results to Excel, you can use CSV-files and automate the entire process by using this function: function Out - Excel ( $path = " $env:temp\$(Get-Date -format 'yyyyMMddHHmmss').csv " ) { $input | Export-Csv $path -UseCulture...
  • Creating Self-Updatable Variables

    If you want a variable to update its content every time you retrieve it, you can assign a breakpoint and an action to it. The action script block will then be executed each time the variable is read: $Global:Now = Set-PSBreakpoint -Variable Now -Mode...
  • Open File Exclusively

    To open a file in a locked state so no one else can open, access, read, or write the file, you can use the low-level .NET methods like this: $path = ' c:\somefile.txt ' # MUST EXIST! $file = [ System.io.File ] :: Open ( $path , ' Open '...
  • Simple Breakpoint

    If you want PowerShell to halt your script at some point, you can simply add this line: $host . EnterNestedPrompt () This will suspend execution and you will get back to the prompt. You can now examine your script variables or even change them. To resume...
  • Locking the Computer

    To lock your computer from PowerShell, remember that you can launch applications, including rundll32.exe, which can be used to call methods from inside DLL files: rundll32.exe user32.dll , LockWorkStation ReTweet this Tip!
  • Extract Text without Regular Expressions

    If you don't like creating Regular Expression patterns, here is a trick so you can easily find and extract text: $text = ' The problem was discussed in KB552356. Mail feedback to tobias @powershell.com ' $words = $text -split ' ' ...
  • Use RegEx to Extract Text

    With Regular Expressions, you can easily extract matching text. Have a look: $text = ' The problem was discussed in KB552356. Mail feedback to tobias @powershell.com ' $pattern = ' KB\d{4,6} ' if ( $text -match $pattern ) { $matches [...
  • Use Select-String with Context

    Select-String can find lines with a specific keyword. It can also include context-relevant lines before and after that line. This will filter the result from ipconfig to focus on your network adapter parameters only: ipconfig | Select-String LAN -context...
  • Launching Applications

    When you launch *.exe-applications with arguments, you may get exceptions because PowerShell may misinterpret the arguments. A better way to do this is using Start-Process and then separate file path and arguments with the parameters -FilePath and -ArgumentList...
1 2 Next >
Copyright 2012 PowerShell.com. All rights reserved.