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
  • 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!
  • Useful Path Manipulation Shortcuts

    All PowerShell Versions Here are a bunch of useful (and simple to use) system functions for dealing with file paths: [ System.IO.Path ] :: GetFileNameWithoutExtension ( ' file.ps1 ' ) [ System.IO.Path ] :: GetExtension ( ' file.ps1 ' ...
  • Finding Minimum and Maximum Values

    All PowerShell Versions To find the smallest and largest item in a range of numbers, use Measure-Object: $list = 1 , 4 , 3 , 1 , 3 , 12 , 990 $result = $list | Measure-Object -Minimum -Maximum $result . Minimum $result . Maximum This works for any input...
  • Combining Results

    All PowerShell Versions Let's assume you want to identify suspicious service states like services that are stopped although their start mode is set to "Automatic", or identify services with ExitCodes that you know are bad. Here is some example...
  • Countdown Hours

    All PowerShell Versions Whether it is a birthday or an important game, you may want PowerShell to tell you just how many hours it is till the event starts. Here is how: $result = New-TimeSpan -End ' 2014-12-25 06:45:00 ' $hours = [ Int ] $result...
  • Using -f Operator to Combine String and Data

    All PowerShell Versions Strings enclosed by double-quotes will resolve variables so it is common practice to use code like this: $name = $host . Name "Your host is called $name." However, this technique has limitations. If you wanted to display...
  • Playing WAV Sounds

    PowerShell 3.0 or later To play a WAV sound file in a background process, PowerShell can use the built-in SoundPlayer class. It accepts a path to a WAV file and lets you then decide whether you want to play the sound once or repeatedly. This would play...
< Previous 1 2 3 4 5 Next > ... Last »
Copyright 2012 PowerShell.com. All rights reserved.