Home > Net >  How to pass all flags in a Powershell New-Alias?
How to pass all flags in a Powershell New-Alias?

Time:10-04

I'm using C:\Users\User\Documents\Powershell\Microsoft.PowerShell_profile.ps1 for some alias commands for my every day work.

Example:

function MyAliasCommand { .\custom\script.ps1 }
New-Alias my MyAliasCommand

It works if I call it from my Powershell using the command "my".

I'm happy.

What it doesn't work is if I need to pass flags to my, example:

my -d one

How to do this?

Is there a way to let powershell pass all my arguments in the function MyAliasCommand?

Something like: function MyAliasCommand { .\custom\script.ps1 $allTheFlags }?

CodePudding user response:

I would do this as follows: ./custom/script.ps1

[CmdletBinding()]
param (
    [Parameter()]
    [string]
    $d
)

Write-Host $d

Then you can write:

function MyCommand {
    & $PSScriptRoot/custom/script.ps1 @args
}

New-Alias myAlias MyCommand

myAlias -d 'one'

Take into account that I used the call operator and splatting.

$args will contain all the parameters you supply.

  • Related