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 Command History as a Tool

    In a previous tip we illustrated how you can copy the previously entered interactive PowerShell commands to your favorite script editor. Here is a function that makes this even easier. If you like it, you may want to put it into your profile script so...
  • Copying Command History

    If you played with PowerShell and suddenly notice that some of the lines of code you just entered actually work, then you may want to copy and paste them into a script editor, save them, or show them to friends. Here is how: Get-History -Count 5 | Select...
  • Finding Dates Between Two Dates

    If you must know how many days are between two dates, you can easily find out by using New-TimeSpan: $startdate = Get-Date $enddate = Get-Date -Date ' 2014-09-12 ' $difference = New-TimeSpan -Start $startdate -End $enddate $difference . Days However...
  • Using Default Parameters

    In PowerShell 3.0, an option was added to define default values for arbitrary cmdlet parameters. This line, for example, would set the default value for the parameter -Path of all cmdlets to a given path: $PSDefaultParameterValues . Add ( ' *:Path...
  • Speeding Up Scripts with StringBuilder

    Often, scripts add new text to existing text. Here is a piece of code that may look familiar to you: Measure-Command { $text = "Hello" for ( $x = 0 ; $x -lt 100000 ; $x ++ ) { $text += "status $x" } $text } This code is particularly...
  • Finding Working Days

    To find all working days in a given month, here is a neat little one-liner: $month = 7 1 . .31 | ForEach-Object { Get-Date -Day $_ -Month $month } | Where-Object { $_ . DayOfWeek -gt 0 -and $_ . DayOfWeek -lt 6 } Simply assign the month to $month (the...
  • Why Directories Have a Size of 1

    Occasionally, you may notice that folders have a length of 1 byte. This was introduced in PowerShell 3.0. In PowerShell 2.0, they did not report back any length. $folder = Get-Item c:\Windows $folder . Length This artifact is caused by yet another feature...
  • Speeding Up Background Jobs

    Background jobs can be a great thing to speed up scripts because they can do things in parallel. However, background jobs only work well if the code you run does not produce large amounts of data - because transporting back the data via XML serialization...
  • Understanding the statement "exit"

    PowerShell supports the keyword "exit" which is a scope-based. It may work much differently than you assumed it would. Let's take a function like this one: function test { ' A ' exit ' B ' } When you save this function in...
  • Using break, continue, and return

    There are two special keywords in PowerShell that you can use in loops: break and continue. With continue, the loop continues but skips the remaining code. When you issue a break, the loop ends prematurely but returns all results. In addition, there is...
  • Dealing with Environment Variables

    To read a Windows environment variable in PowerShell, simply use the prefix "env:": PS> $env:windir C:\Windows PS> $env:USERNAME Tobias Actually, "env:" is a drive, so you can use it to find all (or some) environment variables...
  • Using Nested Hash Tables

    Nested hash tables can be a great alternative to multidimensional arrays. They can be used to store data sets in an easy-to-manage way. Have a look: $person = @ {} $person . Name = ' Weltner ' $person . Id = 12 $person . Address = @ {} $person...
  • Speeding Up Arrays

    When you assign new items to an array often, you may experience a performance problem. Here is a sample that illustrates how you should not do it: Measure-Command { $ar = @ () for ( $x = 0 ; $x -lt 10000 ; $x ++ ) { $ar += $x } } In a loop, an array receives...
  • Using Event Logs Instead of Log Files

    Often, people use file-based logging. There is nothing wrong about that, but using the built-in event log system provided by Windows may be much easier. If you have admin privileges, you can create new event logs any time: New-EventLog -LogName myLog...
  • Reading Registry Values the Easy Way

    With PowerShell, it can be a piece of cake to read out Registry values. Here is your simple code template: $RegPath = ' HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion ' $key = Get-ItemProperty -Path "Registry::$RegPath"...
« First ... < Previous 3 4 5 6 7 Next > ... Last »
Copyright 2012 PowerShell.com. All rights reserved.