August 2011 - Power Tips

Share |

Join PowerShell.com!
Subscribe to Feed

PowerShell eBook
Sign up for
Your PowerTip of the Day:

Powershell Plus


13 Free SQL Tools from Idera


Free Ebook: Mastering Powershell

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
  • Create CSV without Header

    ConvertTo-CSV can create comma separated values (CSV) on the fly but it always adds a new header. To create CSV data without columns, take a look at some piece of code - it removes the first two lines: $filepath = " $env:temp\rawcsv.txt " $processes...
  • 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 '...
  • 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...
  • 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...
  • 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...
  • 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...
  • 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()...
  • 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...
  • 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...
  • 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...
  • When to use Select-Object's -ExpandProperty

    Use Select-Object to determine which information in a result you are interested in. Here are three easy rules for its parameters: Use -Property * when you want to see maximum information: Get-Process | Select-Object -Property * Use -Property a,b,c to...
  • Adding Personal Drives

    In a previous tip we showed you how you can add new drives to easily access your desktop, your cookies or media like music and video. However, when you call that code from within a function, your new drives are gone right after you created them: function...
  • Eliminating Empty Text

    If you wanted to exclude results with empty (text) columns, you can filter based on $null values. This will get you all processes with a valid company information: Where-Object { $_ . Company -ne $null } | Select-Object Name , Company , Description Or...
  • Assigning Two Unique Random Numbers

    If you need to get two random numbers from a given numeric range, and you want to make sure they cannot be the same, simply tell Get-Random to pick two numbers, and assign them to two different variables at the same time: $foreground , $background = Get...
  • Checking Array Content With Wildcards

    In a previous tip we illustrated how -like can work on array. To iterate on that, check out how -like (in contrast to -contains) allows the use of wildcards to check for array elements: PS > ' Peter ' , ' Mary ' , ' Martin '...
1 2 Next >
Copyright 2012 PowerShell.com. All rights reserved.