Showing posts with label PowerShell Tip. Show all posts
Showing posts with label PowerShell Tip. Show all posts

2016/05/08

Create a function/Cmdlet alias using the [Alias()] attribute

Just wanted to share a small PowerShell tips that Kirk Munro found.
You can declare a function or Cmdlet Alias using the following technique:

function Get-This
{
    [CmdletBinding()]
    [Alias("Get-That")]
    PARAM (
        $Param1
    )
    
    Write-Output "Param1 = $param1"
}

Get-That -Param1 "Hello World"


According to Jason Shirk this has been added in PowerShell v4.0:
"Support for the alias attribute on a function or cmdlet (works in C# too!) was added in V4.
It’s most valuable in a binary module because it’s harder to create aliases via IModuleAssemblyInitializer and when you do via that interface"



2015/01/06

PowerShell Tip - PSReadLine shortcuts

If you are using PowerShell everyday like me, you probably started to populate you profile with some neat tools that make you like easier. In my case It's loading some modules or add some functions such as Connect-Office365 or Clx.

PSReadLine is one of the module I've been using for a while now and I can't work without it anymore!!!!

2014/09/28

PowerShell Tip - Escape Regex MetaCharacters


Last week I worked on a Scorch PowerShell script that is looking for duplicate Incident Requests inside SCSM by checking new incoming request and existing ticket already in the system.

One of the script step is to look for a match between two strings, something similar to the following:

$String1 = "Title:[PowerShell Rocks!]"
$String2 = "Title:[PowerShell Rocks!]"

$String1 -match $String2

Straight forward you would think! And at first I was surprised to see the result $FALSE ... yep...to "ass-u-me"...