February 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
  • Listing Domains in Forest

    Here is a function that lists all the domains in your forest: function Get - Domain { $Root = [ ADSI ] "LDAP://RootDSE" try { $oForestConfig = $Root . Get ( "configurationNamingContext" ) } catch { Write-Warning ' You are currently...
  • Turning On Standby-Mode

    To programmatically enter standby mode, you can use native .NET code like this: function Invoke-Standby { Add-Type -AssemblyName System.Windows.Forms [ System.Windows.Forms.Application ] :: SetSuspendState ( 0 , 0 , 0 ) | Out-Null } ReTweet this Tip!
  • Correctly Returning Exit Codes

    When you launch a PowerShell script from outside PowerShell, you may want to return an exit code to the caller so that the caller knows if your script ran ok. You can send back an exit code by using the statement exit xy where “xy” is a numeric...
  • Safely Running PowerShell Scripts

    If you want to run a PowerShell script from outside PowerShell, for example from within a batch file, you probably know that you need to prepend powershell.exe to the script path. But that is not enough. Always add these three parameters to launch your...
  • Adding PowerShell Goodies to Server 2008 R2

    Windows Server 2008 R2 comes with a PowerShell module called ServerManager which in turn allows you to add additional features to the server. Here’s a list of features you might want to install to get the maximum PowerShell experience: First, load...
  • Recording Audio Text Files

    Did you know that PowerShell can record audio messages? All you need is some text. You can then turn the text into spoken language, convert it to a WAV file and play it back or send it to someone. Here’s how: $Path = " $env:temp\file.wav "...
  • Enter Hibernation Mode

    Maybe you are running lengthy tasks at night. Sometimes you may want to place the machine into hibernation once your script is done. Here’s a function that uses a console application to enter hibernation mode: function Invoke-Hibernate { shutdown...
  • Make PowerShell Speak!

    By adding a new system type called System.Speech, you can make PowerShell speak out loudly: Add-Type -AssemblyName System.Speech $synthesizer = New-Object -TypeName System.Speech.Synthesis.SpeechSynthesizer $synthesizer . Speak ( ' Hey, I can speak...
  • Create Group Policy Reports

    Windows Server 2008 R2 comes with the GroupPolicy PowerShell module. You might have to install that feature first before you can use it – run these lines with full Administrator privileges: Import-Module ServerManager Add-WindowsFeature GPMC Once...
  • Admin Privileges Enabled?

    If you want to know whether your script has currently full Administrator privileges, here is an (admittedly long) one-liner that tells you: ( New-Object System.Security.Principal.WindowsPrincipal ([ System.Security.Principal.WindowsIdentity ] :: GetCurrent...
  • Locking Workstation

    If you ever feel the need to lock your interactive session via PowerShell, here’s a function that can do this (and also illustrates how to use low-level API functionality): function Lock-WorkStation { $signature = @" [DllImport("user32...
  • Logging Off

    Stop-Computer and Restart-Computer can shutdown and restart a machine, but there are things they cannot do, for example logging off the current user. Here’s a simple function wrapper that utilizes a standard console application and wraps it into...
  • Check for 64-bit Environment

    In PowerShell v3 (CTP2 publicly available) it is finally a snap to determine whether you are on a 64-bit machine and/or shell. PowerShell 3.0 is able to provide this useful information thanks to .NET Framework 4.0 BTW PS > [ system.environment ] :...
  • 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 --...
1 2 Next >
Copyright 2012 PowerShell.com. All rights reserved.