Power Tips

Share |

Join PowerShell.com!
Subscribe to Feed

PowerShell eBook
Sign up for
Your PowerTip of the Day:

Powershell Plus


13 Free SQL Tools from Idera


Free Ebook: Mastering Powershell

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
  • Stripping Decimals Without Rounding

    When you divide numbers and just want the decimals before the decimal point, you could cast the result to integer. However, this would also round the result: PS > 18 / 5 3 . 6 PS > [ Int ]( 18 / 5 ) 4 To strip off all decimals behind the decimal...
  • The Two Faces of -match

    The -match operator can be extremely useful because it accepts regular expression patterns and extracts the information that matches the pattern: PS > ' PC678 had a problem ' -match ' PC(\d{3}) ' True PS > $matches Name Value --...
  • Finding Email of Logged On User

    In an Active Directory environment, PowerShell can easily find the currently logged on user and retrieve AD information about that user, for example, his or her email address: $searcher = [ adsisearcher ] "(samaccountname=$env:username)" $searcher...
  • Remotely Launching Processes

    Unfortunately, the Start-Process cmdlet has no -ComputerName parameter so you cannot use it to launch processes on remote machines. Use WMI instead! This line will run calc.exe on your local machine: PS > ( Invoke-WmiMethod Win32_Process Create calc...
  • Keeping Remote Programs Running

    When you use PowerShell Remoting (like the Enter-PSSession cmdlet) to connect to another machine and then start a program using Start-Process, the program is automatically associated to your remote session. Once you leave and discard your remote session...
  • 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...
« First ... < Previous 58 59 60 61 62 Next > ... Last »
Copyright 2012 PowerShell.com. All rights reserved.