Home > Software engineering >  How to create a PowerShell alias for [System.Web.HttpUtility]::UrlDecode()?
How to create a PowerShell alias for [System.Web.HttpUtility]::UrlDecode()?

Time:02-10

I already figured out how to use UrlDecode in PowerShell:

PS Z:\> Add-Type -AssemblyName System.Web
PS Z:\> [System.Web.HttpUtility]::UrlDecode("First Line
Second Line ")

I also figured out that I can load System.Web by adding corresponding command to "C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1" so I don't have to do this manually each time I open PowerShell.

However, I still have to type [System.Web.HttpUtility]::UrlDecode each time which is a hassle even when using tab to auto-complete.

Is there a way to create an alias so that instead of

[System.Web.HttpUtility]::UrlDecode("First Line
Second Line")

I can just type

urdc("First Line
Second Line")

or better yet

urdc "First Line
Second Line"

?

CodePudding user response:

Here is how you can create a simple wrapper function using automatic variables $input and $args:

function urdc {
    if($MyInvocation.ExpectingInput) {
        return $input.ForEach({
            [System.Web.HttpUtility]::UrlDecode($_)
        })
    }
    $args.ForEach({
        [System.Web.HttpUtility]::UrlDecode($_)
    })
}

Now you could use it like:

urdc "First Line
Second Line" "First Line
Second Line"

Or from pipeline:

"First Line
Second Line", "First Line
Second Line" | urdc
  • Related