Why this PowerShell alias does not work

aliaspowershell

I am learning PowerShell.

I would like to understand why some aliases in PowerShell 5.0 under Windows 8.1 do not work.

For instance, this command alone works:

Get-WmiObject -Class Win32_WinSAT

But it does not when defined in my $profile as follows:

Set-Alias -Name wei -Value 'Get-WmiObject -Class Win32_WinSAT'

The error message follows:

PS C:\> wei
wei : The term 'Get-WmiObject -Class Win32_WinSAT' is not recognized as the 
name of a cmdlet, function, script file,
or operable program. Check the spelling of the name, or if a path was 
included, verify that the path is correct and
try again.
At line:1 char:1
+ wei
+ ~~~
    + CategoryInfo          : ObjectNotFound: (Get-WmiObject -Class Win32_WinSAT:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

EDIT:

I see the aliases work a little different than in standard Bash on Linux I am used to.

The solution was to simply declare it as a function:

Function wei { Get-WmiObject -Class Win32_WinSAT }

Best Answer

  • If you like to pass other parameters to your alias, you can do this:

    function wei([Parameter(ValueFromRemainingArguments = $true)]$params) {
        & Get-WmiObject -Class Win32_WinSAT $params
    }