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
  • 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...
  • 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 * ...
  • 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...
  • 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...
  • 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 '...
  • 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...
  • 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...
  • 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...
  • 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...
« First ... < Previous 57 58 59 60 61 Next > ... Last »
Copyright 2012 PowerShell.com. All rights reserved.