May 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
  • Copying Large Files with BITS

    You can use Copy-Item or the console applications xcopy and robocopy to copy large files. But did you know that you can also use the BITS service to do this? The BITS service is supposed to download large update files and does so in a very robust way...
  • Matching "Stars"

    Asterisk serve as wildcards, so how would you check for the presence of an asterisk? It's much harder than you might think: PS > ' Test* ' -eq ' * ' False PS > ' Test* ' -like ' ** ' False PS > ' Test '...
  • Extract Paths from Strings Like Environment Variables

    If you'd like to add a path to the %PATH% environment variable, that's easy: PS > $env:path += ' ;c:\newpath ' But how would you remove a path? Here's a clever way: PS > (( $env:path -split ' ; ' ) -ne ' C:\Windows...
  • Clearing WinEvent Logs

    With Get-WinEvent you can access the various Windows log files such as this one: PS > Get-WinEvent Microsoft - Windows - WinRM / Operational There is no cmdlet to actually clear such event log, though. With this line, you can: PS > [ System.Diagnostics...
  • Adding Clock to PowerShell Console

    Maybe you'd like to include dynamic information such as the current time into the title bar of your PowerShell console. You could update the console title bar inside your prompt function, but then the title bar would only get updated each time you...
  • Integrating WhoAmI Into PowerShell

    There is a cool little tool called whoami.exe which is part of Windows ever since Windows Vista. Since it can provide results not just in plain text but also as comma separated values, it is very easy to integrate into PowerShell. For example, to find...
  • Getting Group Memberships

    If you'd like to know in which groups you are member, here's a simple piece of code that returns the list of groups you belong to: PS > $User = [ System.Security.Principal.WindowsIdentity ] :: GetCurrent () PS > $User . Groups | ForEach...
  • Who am I?

    If you'd like to know your current user account, of course you can query environment variables like this: PS > $env:userdomain PS > $env:username You get a lot more information including your security identifier (SID) by using the appropriate...
  • Optimizing PowerShell Performance

    PowerShell is loading .NET assemblies. These assemblies can be precompiled using the tool ngen.exe which improves loading times (because the DLLs no longer have to be compiled each time they are loaded). Before you think about optimizing the DLLs PowerShell...
  • Get Localized Month Names

    To get a list of month names, you could use this line: PS > [ System.Enum ] :: GetNames ([ System.DayOfWeek ]) Sunday Monday Tuesday Wednesday Thursday Friday Saturday However, this returns a culture-neutral list which is not returning the month names...
  • Creating Symmetric Array

    By default, PowerShell uses jagged arrays. To create conventional symmetric arrays, here's how: PS > $array = New-Object ' Int32[,] ' 2 , 2 This creates a two-dimensional array of Int32 numbers. Note that each dimension starts with 0, so...
  • Finding Domain Controllers

    If your computer is logged on to an Active Directory, here is some code to get to your domain controllers. Note that this will raise errors if you are currently not logged on to a domain. PS > $Domain = [ System.DirectoryServices.ActiveDirectory.Domain...
  • Executing Commands in Groups

    In traditional batch files, you can use "&&" to execute a second command only if the first one worked. In PowerShell, the same can be achieved by using the try/catch construct. You just need to know some little tricks. Take a look at...
  • Listing All WMI Namespaces

    WMI is organized into namespaces which work similar to subfolders. Here's a line that lists all namespaces you got: PS > Get-WmiObject -Query "Select * from __Namespace" -Namespace Root | Select-O bject -ExpandProperty Name Next, you...
  • Formatting XML Files

    Here's a cool little XML formatting tool. It takes the path to any XML file and allows you to specify an indent. Then, it saves the file as new XML file with the indentation you specified. Here's the code for the function: function Format-Xml...
1 2 Next >
Copyright 2012 PowerShell.com. All rights reserved.