January 2012 - 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
  • 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...
  • Create Own Driver Tool

    Thanks to Peter Bishop, here's an enhancement to one of our earlier tips. It turns the command line output delivered by driverquery.exe into a nice info window. Driverquery.exe /v /FO CSV | ConvertFrom-Csv | Select-Object ' Display Name '...
  • Ignoring Empty Lines

    To read in a text file and skip blank lines, try this: $file = ' c:\sometextfile.txt ' Get-Content $file | Where-Object { $_ . Trim () -ne '' } It will omit empty lines, lines with only blanks and lines with only tabs. ReTweet this Tip...
  • Removing Characters at the Beginning of Text

    To remove text at the beginning of a sentence rather than somewhere inside the sentence, use the operator -replace and the anchor '^'. Check out the difference: PS > ' PC101 is the PC we are overhauling ' -replace ' PC ' , ''...
1 2 Next >
Copyright 2012 PowerShell.com. All rights reserved.