PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Forum experts available to answer your PowerShell questions

Power Tips

Power Tips

Syndication

  • Finding Uppercase Characters

    If you'd like to find uppercase characters, you could use regular expressions. However, you would then provide a list of uppercase characters to check against. A more flexible way is to use the .NET function IsUpper(). Here is a sample: it scans a...
  • Using Green Checkmarks in Console Output

    In a previous tip you have seen that the PowerShell console supports all characters available in a TrueType font. You just need to convert the character code to the type "Char". Here is a more advanced example that uses splatting to insert a...
  • Using Symbols in Console Output

    Did you know that console output can contain special icons like checkmarks? All you need to do is set the console to a TrueType font like "Consolas". To display special characters, use the decimal or hexadecimal character code, for example:...
  • Test Nested Depth

    When you call a function, PowerShell increases the nest level. When a function calls another function, or script, this will again increase the nest level. Here is a function that can tell you the current nest level of your code: function Test-NestLevel...
  • Aborting Pipeline

    Sometimes you might want to abort a pipeline when a certain condition is met. Here is a creative way of doing this. It works all the way back to PowerShell 2.0. Take a look at the sample code: filter Stop-Pipeline { param ( [ scriptblock ] $condition...
  • "Continue" and Labels

    When you use the "Continue" statement inside a loop, you can skip the remainder of this loop iteration, and continue with the next. "Break" works similar, but aborts a loop and skips all remaining iterations. This raises the question...
  • Get Memory Consumption

    To get a rough understanding how much memory a script takes, or how much memory PowerShell puts aside when you store results in a variable, here is a helper function: #requires -Version 2 $script:last_memory_usage_byte = 0 function Get-MemoryUsage { ...
  • Use Closures to Lock Variables to Script Blocks

    When you use variables inside a script block, the variables are evaluated when you run the script block. To lock variable content, you can create a new "Closure". Once you do this, the script block takes the variable values that were assigned...
  • Mutually Exclusive Parameters (Part 2)

    Mutually exclusive parameters in PowerShell functions use the "ParameterSetName" attribute to assign parameters to different parameter sets (or groups of parameters). A little known fact is that you can assign multiple parameter set names to...
  • Mutual Exclusive Parameters

    Sometimes, PowerShell functions have parameters that should be mutually exclusive: the user should only be able to use either one, not both. To create mutually exclusive parameters, assign them to different parameter sets, and make sure you define a default...
  • Parsing PowerShell Scripts

    If you'd like to create your own color-coded PowerShell scripts, for example formatting them in HTML, here is a sample that gets you started. This sample takes the currently displayed script inside the ISE editor, and asks the PowerShell parser to...
  • Aborting the Pipeline

    If you know beforehand how many results you expect from a pipeline, you can use Select-Object to stop the upstream cmdlets. This can save a lot of time. This example tries to find the first instance of explorer.exe inside the Windows folder. Because of...
  • Passing Arrays to Pipeline

    If a function returns more than one value, PowerShell wraps them in an array. However, if you pass the results to another function inside a pipeline, the pipeline automatically "unwraps" the array and processes one array element at a time. If...
  • Free PowerShell Module for Admins

    One feedback we got on a previous tip directed us to "Carbon", a free PowerShell module crammed with useful PowerShell functions. One is Get-IPAddress which returns all IP addresses assigned to your computer. You can find more information about...
  • Converting CSV to Excel File

    PowerShell can easily create CSV files using Export-Csv, and if Microsoft Excel is installed on your system, PowerShell can then have Excel convert the CSV file to a native XLSX Excel file. Here is some sample code. It uses Get-Process to get some data...
1 2 3 4 5 Next > ... Last »
Copyright 2012 PowerShell.com. All rights reserved.