简体   繁体   中英

Creating users with Powershell

I am trying to learn PowerShell.

$newuser = ‘ash’, ‘bob’, ‘charlie’, ‘david’

$newuser | foreach({New-LocalUser -Name $_newuser -Password “123123” -Description “Students”})

What I am trying to do is to create new local accounts with each name on the list, $newuser , with the password and Description I have.

I have googled a lot and tried other things, but no luck.

Do I need to create a list using this syntax? $newuser = @(ash, bob, charlie, david)

Also I am confused with what to put after -Name . Tried something like -Name $newuser , -Name $__.newuser , -Name $_.($newuser) , etc. I have been looking for any method to call each object from the $newuser for creating each user account.

Any suggestion will be appreciated!!

You're close to getting where you want!

Declaring Arrays: You'll notice that when you try to use $newuser = @(ash, bob, charlie, david) in PowerShell ISE or VSCode, you'll get a syntax error. This is because you need to place quotes around each item. The way you've done it originally is no different in practice. It is simply up to how you want to define arrays. Sometimes with large arrays that expand multiple lines, it can be easier to keep track of where it ends when wrapped with @()

Understanding your variables: You can test the two array declaration methods by trying both ways, and using the GetType() method like so: $newuser.GetType()

Naming Conventions: Try to use plurals for variable names when dealing with something that contains multiple objects, like an array. So in this case, $newuser should be $newusers , and try to make your variables descriptive of what they contain. (You've done that nicely here)

$PSItem and $_: When you iterate over an array, there is a nifty PowerShell variable called $PSItem , also called by alias $_ . This variable contains the current object you are iterating over. See the example below for how I might do something like this, but also keep in mind there are multiple ways to accomplish things in PowerShell most of the time.

Passwords: The cmdlet you are using here will not take a string as a password, but instead will require a SecureString. You can force the string to a SecureString in the script, but only do this as practice. The best thing to do is have a file that you call from and shred afterwards or external password manager that you call upon when needed to pass the value. PlainTexting the password is NEVER a good idea .

Method 1

$newUsers = ‘ash’, ‘bob’, ‘charlie’, 'david’
$securePassword = ConvertTo-SecureString '123123' -AsPlainText -Force 

foreach ($user in $newUsers) {
    New-LocalUser -Name $user -Password $securePassword -Description 'Students'
}

Method 2

$securePassword = ConvertTo-SecureString '123123' -AsPlainText -Force 

‘ash’, ‘bob’, ‘charlie’, 'david’ | ForEach-Object {New-LocalUser -Name $PSItem -Password $securePassword -Description 'Students'}

This is mostly personal preference, or you'd follow the standard your team uses, but I'd say the first is what I would use in a script, as I find it a bit more readable, and the second is something I would do on the fly in a terminal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM