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
  • 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...
  • 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...
  • 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 '...
  • 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 '...
  • Appending CSV Data

    To append a CSV file with new data, first of all make sure the type of data you append is the same type of data already in a file (or else column names will not match). This creates a list of unique running processes (so it will not list duplicate process...
  • Managing File System Tasks

    If you need to list all cmdlets that deal with file system-related tasks, try this: Get-Command -Noun item * , path Many of these cmdlets have historic aliases that will help you guess what they are doing: Get-Alias -Definition *- item * , *- path * ...
  • 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.