Why this PowerShell alias does not work?











up vote
1
down vote

favorite












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 }









share|improve this question




















  • 1




    Your alias equivalent to & 'Get-WmiObject -Class Win32_WinSAT'.
    – PetSerAl
    Jul 19 '17 at 5:01















up vote
1
down vote

favorite












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 }









share|improve this question




















  • 1




    Your alias equivalent to & 'Get-WmiObject -Class Win32_WinSAT'.
    – PetSerAl
    Jul 19 '17 at 5:01













up vote
1
down vote

favorite









up vote
1
down vote

favorite











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 }









share|improve this question















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 }






powershell alias






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 19 '17 at 10:41

























asked Jul 19 '17 at 4:48









Vlastimil

1,11311333




1,11311333








  • 1




    Your alias equivalent to & 'Get-WmiObject -Class Win32_WinSAT'.
    – PetSerAl
    Jul 19 '17 at 5:01














  • 1




    Your alias equivalent to & 'Get-WmiObject -Class Win32_WinSAT'.
    – PetSerAl
    Jul 19 '17 at 5:01








1




1




Your alias equivalent to & 'Get-WmiObject -Class Win32_WinSAT'.
– PetSerAl
Jul 19 '17 at 5:01




Your alias equivalent to & 'Get-WmiObject -Class Win32_WinSAT'.
– PetSerAl
Jul 19 '17 at 5:01










2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










Normally PowerShell tries to use the first space to separate the command from the parameters. However, you can use a string to specify that a space is just part of a file. This essentially lets you treat the space like a non-special character, and allows you to treat something like 'C:Program FilesWindows NTAccessoriesnotepad.exe' as if it was one word, not two.



That's essentially what you're doing. PowerShell cannot find a command named 'Get-WmiObject -Class Win32_WinSAT', because there is no such command. (The command in question is simply 'Get-WmiObject', not 'Get-WmiObject -Class Win32_WinSAT'.






share|improve this answer





















  • So, I have now identified the problem. I realize I haven't provided a workaround/solution. Being that I am supposed to be at work in less than 5 hours and should get some more sleep before then, I will allow someone else that pleasure. Hopefully my comment helped identify the right track of what needed to be solved.
    – TOOGAM
    Jul 19 '17 at 10:10


















up vote
1
down vote













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
}





share|improve this answer





















    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "3"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1231175%2fwhy-this-powershell-alias-does-not-work%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    Normally PowerShell tries to use the first space to separate the command from the parameters. However, you can use a string to specify that a space is just part of a file. This essentially lets you treat the space like a non-special character, and allows you to treat something like 'C:Program FilesWindows NTAccessoriesnotepad.exe' as if it was one word, not two.



    That's essentially what you're doing. PowerShell cannot find a command named 'Get-WmiObject -Class Win32_WinSAT', because there is no such command. (The command in question is simply 'Get-WmiObject', not 'Get-WmiObject -Class Win32_WinSAT'.






    share|improve this answer





















    • So, I have now identified the problem. I realize I haven't provided a workaround/solution. Being that I am supposed to be at work in less than 5 hours and should get some more sleep before then, I will allow someone else that pleasure. Hopefully my comment helped identify the right track of what needed to be solved.
      – TOOGAM
      Jul 19 '17 at 10:10















    up vote
    1
    down vote



    accepted










    Normally PowerShell tries to use the first space to separate the command from the parameters. However, you can use a string to specify that a space is just part of a file. This essentially lets you treat the space like a non-special character, and allows you to treat something like 'C:Program FilesWindows NTAccessoriesnotepad.exe' as if it was one word, not two.



    That's essentially what you're doing. PowerShell cannot find a command named 'Get-WmiObject -Class Win32_WinSAT', because there is no such command. (The command in question is simply 'Get-WmiObject', not 'Get-WmiObject -Class Win32_WinSAT'.






    share|improve this answer





















    • So, I have now identified the problem. I realize I haven't provided a workaround/solution. Being that I am supposed to be at work in less than 5 hours and should get some more sleep before then, I will allow someone else that pleasure. Hopefully my comment helped identify the right track of what needed to be solved.
      – TOOGAM
      Jul 19 '17 at 10:10













    up vote
    1
    down vote



    accepted







    up vote
    1
    down vote



    accepted






    Normally PowerShell tries to use the first space to separate the command from the parameters. However, you can use a string to specify that a space is just part of a file. This essentially lets you treat the space like a non-special character, and allows you to treat something like 'C:Program FilesWindows NTAccessoriesnotepad.exe' as if it was one word, not two.



    That's essentially what you're doing. PowerShell cannot find a command named 'Get-WmiObject -Class Win32_WinSAT', because there is no such command. (The command in question is simply 'Get-WmiObject', not 'Get-WmiObject -Class Win32_WinSAT'.






    share|improve this answer












    Normally PowerShell tries to use the first space to separate the command from the parameters. However, you can use a string to specify that a space is just part of a file. This essentially lets you treat the space like a non-special character, and allows you to treat something like 'C:Program FilesWindows NTAccessoriesnotepad.exe' as if it was one word, not two.



    That's essentially what you're doing. PowerShell cannot find a command named 'Get-WmiObject -Class Win32_WinSAT', because there is no such command. (The command in question is simply 'Get-WmiObject', not 'Get-WmiObject -Class Win32_WinSAT'.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jul 19 '17 at 10:09









    TOOGAM

    11.3k32442




    11.3k32442












    • So, I have now identified the problem. I realize I haven't provided a workaround/solution. Being that I am supposed to be at work in less than 5 hours and should get some more sleep before then, I will allow someone else that pleasure. Hopefully my comment helped identify the right track of what needed to be solved.
      – TOOGAM
      Jul 19 '17 at 10:10


















    • So, I have now identified the problem. I realize I haven't provided a workaround/solution. Being that I am supposed to be at work in less than 5 hours and should get some more sleep before then, I will allow someone else that pleasure. Hopefully my comment helped identify the right track of what needed to be solved.
      – TOOGAM
      Jul 19 '17 at 10:10
















    So, I have now identified the problem. I realize I haven't provided a workaround/solution. Being that I am supposed to be at work in less than 5 hours and should get some more sleep before then, I will allow someone else that pleasure. Hopefully my comment helped identify the right track of what needed to be solved.
    – TOOGAM
    Jul 19 '17 at 10:10




    So, I have now identified the problem. I realize I haven't provided a workaround/solution. Being that I am supposed to be at work in less than 5 hours and should get some more sleep before then, I will allow someone else that pleasure. Hopefully my comment helped identify the right track of what needed to be solved.
    – TOOGAM
    Jul 19 '17 at 10:10












    up vote
    1
    down vote













    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
    }





    share|improve this answer

























      up vote
      1
      down vote













      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
      }





      share|improve this answer























        up vote
        1
        down vote










        up vote
        1
        down vote









        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
        }





        share|improve this 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
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 1 at 15:10









        ahmadali shafiee

        1931111




        1931111






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Super User!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1231175%2fwhy-this-powershell-alias-does-not-work%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

            Mangá

             ⁒  ․,‪⁊‑⁙ ⁖, ⁇‒※‌, †,⁖‗‌⁝    ‾‸⁘,‖⁔⁣,⁂‾
”‑,‥–,‬ ,⁀‹⁋‴⁑ ‒ ,‴⁋”‼ ⁨,‷⁔„ ‰′,‐‚ ‥‡‎“‷⁃⁨⁅⁣,⁔
⁇‘⁔⁡⁏⁌⁡‿‶‏⁨ ⁣⁕⁖⁨⁩⁥‽⁀  ‴‬⁜‟ ⁃‣‧⁕‮ …‍⁨‴ ⁩,⁚⁖‫ ,‵ ⁀,‮⁝‣‣ ⁑  ⁂– ․, ‾‽ ‏⁁“⁗‸ ‾… ‹‡⁌⁎‸‘ ‡⁏⁌‪ ‵⁛ ‎⁨ ―⁦⁤⁄⁕