PowerShell - CommandNotFoundException

Eddie Eagle 1 Reputation point
2021-12-06T12:59:01.577+00:00

I have a strange error with the PowerShell and a simple command:

New-LocalGroup -Description "Test Usergroup" -Name Testsusers

When I open the Powershell from CMD-box or from Start menu and run the command everything works fine (if I start with elevated rights). If I start the Shell without elevated rights I get the correct message "access denied...".

I want to run this command from an installation script (Inno Setup). The setup runs with elevated privileges, the powershell starts, but the script will not be executed. I have added to the command line the parameter "-NoExit" so that I can see what is happening. So I see that the command "New-LocalGroup" is unknown (CommandNotFoundException).
How can it be that on the same Windows 10 X64, with the same user the command is once known and once not?

Windows for business | Windows Server | User experience | PowerShell
Windows for business | Windows Client for IT Pros | User experience | Other
0 comments No comments
{count} votes

10 answers

Sort by: Most helpful
  1. Clément BETACORNE 2,496 Reputation points
    2021-12-09T15:55:21.533+00:00

    Hello,

    Maybe you try the old way with [ADSI] object :
    https://devblogs.microsoft.com/scripting/adding-local-users-to-local-groups/

    Regards,

    1 person found this answer helpful.
    0 comments No comments

  2. Andreas Baumgarten 123.5K Reputation points MVP Volunteer Moderator
    2021-12-06T13:53:26.083+00:00

    Hi @Eddie Eagle ,

    you could give it a try to load the PS module in your script first: Import-Module localaccount

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

  3. Eddie Eagle 1 Reputation point
    2021-12-06T14:15:09.693+00:00

    When I try this I get the following message:
    Import modules : The specified module "localaccount" was not loaded because no valid module file was found in any module directory.

    Best,
    Thomas


  4. Eddie Eagle 1 Reputation point
    2021-12-06T14:31:13.27+00:00

    No, I start it as "normal" admin, with elevated rights.
    Thomas


  5. Eddie Eagle 1 Reputation point
    2021-12-06T14:41:07.847+00:00

    This gives exact the same error.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.