PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Forum experts available to answer your PowerShell questions

Power Tips

Power Tips

Syndication

  • Text Splitting

    All PowerShell Versions With the –split operator, you can split text at given locations. The operator expects a regular expression, so if you just want to split using plain text expressions, you need to escape your split text. Here is an example...
  • Replacing Duplicate Spaces

    All PowerShell Versions To eliminate any duplicate space, try this regular expression: PS> '[ Man, it works! ]' -replace '\s{2,}', ' ' [ Man, it works! ] You can use this approach also to convert fixed-width text tables to CSV...
  • Creating TinyURLs

    All PowerShell Versions You probably have heard about shortening long URLs. There are plenty of free services available. Here is a script that turns any URL into a tiny URL: $OriginalURL = ' http://www.powertheshell.com/isesteroids2 ' $url = "http...
  • Finding PowerShell Functions

    PowerShell 3.0 and later To quickly scan your PowerShell script repository and find all files that have a given function in them, try this filter: filter Find-Function { $path = $_ . FullName $lastwrite = $_ . LastWriteTime $text = Get-Content -Path ...
  • Dumping Service State Information

    All PowerShell Versions If you would like to save the results of a PowerShell command to disk so that you can take it with you to another machine, here is a simple way: $Path = " $env:temp\mylist.xml " Get-Service | Add-Member -MemberType NoteProperty...
  • Comparing Service Configuration

    PowerShell 3 and later Provided you have PowerShell remoting up and running on two servers, here is a simple script that illustrates how you can get the state of all services from each server and then calculate the differences between the two servers...
  • Download PowerShell Language Specification

    All PowerShell Versions With PowerShell, it is really easy to download files from the Internet. Here is the code that downloads the PowerShell language specification--a great Word document full of PowerShell gems and internals--automatically to your machine...
  • Waiting for a Keystroke

    All PowerShell versions, PowerShell Console only To keep the PowerShell console open when a script is done, you may want to add a “Press Any Key” statement. Here is a way how you can implement this: Write-Host ' Press Any Key! ' -NoNewline...
  • Finding Errors in Scripts

    All PowerShell Versions It’s never been easier to find scripts with syntax errors in them. Just use this filter: filter Test-SyntaxError { $text = Get-Content -Path $_ . FullName if ( $text . Length -gt 0 ) { $err = $null $null = [ System.Management...
  • Testing Whether Text Contains Upper Case

    All PowerShell Versions Use regular expressions to check whether a string contains at least one upper case letter: $text1 = ' this is all lower-case ' $text2 = ' this is NOT all lower-case ' $text1 -cmatch ' [A-Z] ' $text2 -cmatch...
  • Getting the Number of Lines in a String

    All PowerShell Versions Here is a clever trick how to find out how many lines a string (not a string array!) contains: $text = @' This is some sample text Let's find out the number of lines. '@ $text . Length - $text . Replace ( "`n"...
  • Removing Illegal Path Characters

    All PowerShell versions In path names, some characters like colons or quotes are illegal. If your script derives path names from some pieces of information, you may want to make sure that the resulting path name is legal. Here is a function that takes...
  • Discarding Results

    All PowerShell Versions Since PowerShell returns anything that commands leave behind, it is particularly important in PowerShell scripts to discard any result that you do not want to return. There are many way to achieve this, and here are the two most...
  • Optional and Mandatory at the Same Time

    All PowerShell Versions Can parameters be optional and mandatory at the same time? They can, based on context. A parameter can be mandatory when other parameters are present, and optional otherwise. function Connect-Somewhere { [ CmdletBinding ( DefaultParameterSetName...
  • Important Math Functions

    All PowerShell Versions Here are the four most important Math functions needed by administration scripts: [ Math ] :: Floor ( 4.9 ) [ Math ] :: Ceiling ( 3.2 ) [ Math ] :: Max ( 3 , 8 ) [ Math ] :: Min ( 3 , 8 ) ReTweet this Tip!
« First ... < Previous 3 4 5 6 7 Next > ... Last »
Copyright 2012 PowerShell.com. All rights reserved.