ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Managing Windows Local Users with PowerShell

    IT Discussion
    windows sam windows administration system administration windows administration powershell shell command line cli
    3
    14
    1.2k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • scottalanmillerS
      scottalanmiller
      last edited by scottalanmiller

      Now that PowerShell has become the primary way to manage Windows machines, tasks such as local user management that were traditionally done with net user command can be done right from inside of PowerShell. The commands for managing local users are part of the LocalAccounts commandlet.

      It is important to note that these commandlets are not part of PowerShell proper and are not available in all PowerShell versions. Also, while this is the "PowerShell way" to do user management, older methods like the net commands are stand alone utilities and work equally well from PowerShell as from any other calling method.

      List Local Users

      Get-LocalUser
      

      Get Details of Specific Local User

      Get-LocalUser "sally"
      

      Create a New Local User

      This command is quite a bit more unnecessarily obtuse than its use or Linux counterparts, in an attempt to enforce potentially unwanted password decisions, simply passing the password is not possible with this command. So we have to do two steps instead of just one. Note this can make PS very hard to use in some remote command execution systems where variables are lost between commands.

      First we have to set the password in a variable, before we can create the user.

      Creating a password interactively:

      $Password = Read-Host -AsSecureString
      

      Or creating a password directly in the command:

      $TempPass = "mySecret"
      $Password = ConvertTo-SecureString -AsPlainText $password -Force
      

      Now that we have set the variable that we will use, we can pass it to the user creation command.

      New-LocalUser -Name "sally" -Password $Password
      

      Delete a Local User

      Remove-LocalUser -Name sally
      

      LocalAccounts Command Guide on Microsoft


      Part of a series on Windows Systems Administration by Scott Alan Miller

      WrCombsW ObsolesceO 2 Replies Last reply Reply Quote 2
      • WrCombsW
        WrCombs @scottalanmiller
        last edited by

        Part of a series on WIndows Systems Administration by Scott Alan Miller

        Can we get a link to this?

        scottalanmillerS 1 Reply Last reply Reply Quote 0
        • scottalanmillerS
          scottalanmiller @WrCombs
          last edited by

          @WrCombs said in Managing Windows Local Users with PowerShell:

          Part of a series on WIndows Systems Administration by Scott Alan Miller

          Can we get a link to this?

          I've not put the ToC together yet. But the tag works.

          WrCombsW 1 Reply Last reply Reply Quote 1
          • WrCombsW
            WrCombs @scottalanmiller
            last edited by

            @scottalanmiller said in Managing Windows Local Users with PowerShell:

            @WrCombs said in Managing Windows Local Users with PowerShell:

            Part of a series on WIndows Systems Administration by Scott Alan Miller

            Can we get a link to this?

            I've not put the ToC together yet. But the tag works.

            sweet - thanks.
            I have some interest in Windows Administration , Thanks

            scottalanmillerS 1 Reply Last reply Reply Quote 0
            • scottalanmillerS
              scottalanmiller @WrCombs
              last edited by

              @WrCombs said in Managing Windows Local Users with PowerShell:

              @scottalanmiller said in Managing Windows Local Users with PowerShell:

              @WrCombs said in Managing Windows Local Users with PowerShell:

              Part of a series on WIndows Systems Administration by Scott Alan Miller

              Can we get a link to this?

              I've not put the ToC together yet. But the tag works.

              sweet - thanks.
              I have some interest in Windows Administration , Thanks

              Me too 🙂

              1 Reply Last reply Reply Quote 1
              • CloudKnightC
                CloudKnight
                last edited by

                so ugly compared to using bash in linux...I just hate the way powershell syntax is.

                WrCombsW 1 Reply Last reply Reply Quote 1
                • WrCombsW
                  WrCombs @CloudKnight
                  last edited by

                  @StuartJordan said in Managing Windows Local Users with PowerShell:

                  so ugly compared to using bash in linux...I just hate the way powershell syntax is.

                  What does the syntax in linux "using bash" look like?

                  CloudKnightC scottalanmillerS 2 Replies Last reply Reply Quote 0
                  • CloudKnightC
                    CloudKnight @WrCombs
                    last edited by CloudKnight

                    @WrCombs In regards to adding users on Linux..not adding Windows users.

                    1 Reply Last reply Reply Quote 1
                    • CloudKnightC
                      CloudKnight
                      last edited by

                      even the net user command looks cleaner:

                      "net localgroup administrators username /add"

                      But yep, powershell is the future and that is now defunct....

                      scottalanmillerS 1 Reply Last reply Reply Quote 3
                      • CloudKnightC
                        CloudKnight
                        last edited by

                        the only bonus, you do get tab completion is powershell...mainly because you need it for the long ass commands lol

                        scottalanmillerS 1 Reply Last reply Reply Quote 2
                        • scottalanmillerS
                          scottalanmiller @CloudKnight
                          last edited by

                          @StuartJordan said in Managing Windows Local Users with PowerShell:

                          the only bonus, you do get tab completion is powershell...mainly because you need it for the long ass commands lol

                          Self fulfilling need 🙂

                          1 Reply Last reply Reply Quote 1
                          • scottalanmillerS
                            scottalanmiller @CloudKnight
                            last edited by

                            @StuartJordan said in Managing Windows Local Users with PowerShell:

                            even the net user command looks cleaner:
                            "net localgroup administrators username /add"

                            Yeah, faster, simpler, easier, more obvious, more memorable.

                            1 Reply Last reply Reply Quote 2
                            • scottalanmillerS
                              scottalanmiller @WrCombs
                              last edited by scottalanmiller

                              @WrCombs said in Managing Windows Local Users with PowerShell:

                              @StuartJordan said in Managing Windows Local Users with PowerShell:

                              so ugly compared to using bash in linux...I just hate the way powershell syntax is.

                              What does the syntax in linux "using bash" look like?

                              So definitely "using Bash" needs quotes, it's "in standard UNIX" and Bash just calls it. It's really Windows vs. Linux here rather than PowerShell vs Bash. But the "official" Windows way is this PS CmdLet mess. The standard Linux way looks like this...

                              useradd sally
                              

                              Then to add a password all at once is more complex. net user is the winner of simplicity.

                              Here is how you do a single line add with useradd in Linux...

                              useradd  -p $(echo mysecret | openssl passwd -1 -stdin) sally
                              
                              1 Reply Last reply Reply Quote 1
                              • scottalanmillerS
                                scottalanmiller
                                last edited by

                                Topic has been forked, please keep discussions of OS comparisons to a different thread.

                                1 Reply Last reply Reply Quote 0
                                • 1 / 1
                                • First post
                                  Last post