Power Tips

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
  • Converting TABs to Spaces

    When you want to publish PowerShell code, you may want to make sure that all TAB characters are converted to one or more spaces to save space. Here's the code that turns TAB characters to spaces: PS > $text = "This is a`tTAB-delimited`tText"...
  • Removing Multiple White Spaces

    Removing multiple white spaces from text is easy in PowerShell. Simply use -replace operator and look for whitespaces ("\s") that occur one or more time ("+"), then replace them all with just one whitespace: PS > ' [ Man, it...
  • Removing Options from Command String (Enhancement)

    In a previous tip we showed how you can remove options from a command line text using -replace. Here is a more versatile approach. It will remove any character or word that starts with "/" and then remove all trailing spaces as well: PS >...
  • Remove Options from Command String

    Let's assume you'd like to remove all options from a raw text command such as this one: xcopy "C:\Some Folder" "C:\Some New Folder Name" /y /r /Q Since all options start with "/" and are a single character, you can...
  • Using Shared Variables

    By default, all variables created in functions are local, so they only exist within that function and all functions that are called from within this function. Sometimes, you'd like to examine variables defined in a function after that function executed...
  • Catching Errors

    In forums, people often get confused with error handling. For example, this code does not call the error handler. Instead, the red PowerShell error message pops up: try { Remove-Item \\ $name \ c $\ windows \ temp \ filename.exe } catch { Write "Not...
  • Analyzing System Restarts (Alternative)

    In a previous tip we showed how to use Get-EventLog to extract all events related to system reboots. In PowerShell v2, a new cmdlet called Get-WinEvent was added. With it, you can not only access and read the "classic" event logs but also the...
  • Analyzing System Restarts

    To find out when a system restarted and why, use the below code to extract the relevant information from the System event log: Get-EventLog -LogName System -ComputerName storage1 | where { $_ . EventId -eq 1074 } | ForEach-Object { $rv = New-Object PSObject...
  • Sending Emails Securely (via SSL)

    In a previous tip we showed how to use the Send-MailMessage cmdlet to send off emails and preserve special characters by using UTF8 encoding. When you try this with your freemailer, you may run into issues where your SMTP server complains about needing...
  • Sending Emails with Special Characters

    PowerShell has built-in support for sending emails: Send-MailMessage! All you need is an SMTP server. However, with standard encoding you may run into issues where special characters are mangled. Use the -Encoding parameter and specify UTF8 to preserve...
  • Check Active Internet Connection

    If your machine is connected to the Internet more than once, let's say cabled and wireless at the same time, which connection is used? Here's a function that tells you (thanks for inspiration to fellow MVP Richard Siddaway): function test-ipmetric...
  • Use WMI and WQL!

    WMI is a great information resource, and Get-WmiObject makes it easy to retrieve WMI instances. First, use -List parameter to find WMI class names. For example, find classes that deal with network: Get-WmiObject -List Win32_ * network * Next, pick one...
  • Convert to Numeric

    Whenever PowerShell asks for user input or reads text file content, the results are text strings. If you expect numbers and want to calculate, make sure you cast them to a numeric format. Have a look: PS > $number = Read-Host ' Enter a number '...
  • Read/Delete/Move Every X. File

    Occasionally, you may want to act on every 2nd or 3rd file in a folder (or line in a file). The easiest way to identify every x. element is to use the "%" (modulus) operator. This will list every 5th file in the Windows folder (for what it's...
  • Managing Internet Cookies

    Ever wondered what Internet sites store inside cookies when you visit them? This line will dump all cookies: PS > dir ([ system.environment ] :: GetFolderPath ( ' Cookies ' )) Cookies are text files. In older versions of IE, the file name contains...
1 2 3 4 5 Next > ... Last »
Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.