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
  • 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...
  • System Uptime

    All PowerShell Versions Windows starts a high definition counter each time it boots, and this counter will return the milliseconds the system runs: $millisecondsUptime = [ Environment ] :: TickCount "I am up for $millisecondsUptime milliseconds!"...
  • Finding Attached USB Sticks

    All PowerShell Versions If you'd like to know whether there are currently USB storage devices attached to your computer, WMI can help: Get-WmiObject -Class Win32_PnPEntity | Where-Object { $_ . DeviceID -like ' USBSTOR* ' } This returns all...
  • Test Service Responsiveness

    All PowerShell Versions To test whether a particular service is still responding, use a clever trick. First, ask WMI for the service you want to check. WMI will happily return the process ID of the underlying process. Next, look up this process, and the...
  • WMI Device Inventory

    All PowerShell Versions The WMI service can report plenty of details about the computer hardware. Typically, each type of hardware is represented by its own WMI class. It's not easy to find out the names of such hardware classes, though. Since all...
  • Get Sleep and Hibernation Times

    All PowerShell Versions If you want to find out whether a computer is frequently put into sleep or hibernation mode, here is a function that reads the appropriate event log entries and returns a table with details, reporting when the computer was put...
< Previous 1 2 3 4 5 Next > ... Last »
Copyright 2012 PowerShell.com. All rights reserved.