Question

In: Computer Science

Hi, Quick question regarding writing a Powershell script and creating a windows non-admin user account, is...

Hi,

Quick question regarding writing a Powershell script and creating a windows non-admin user account, is this how you do it? Wanna know if I'm on the right track please and thanks!

$Username = "AnnieSue"

$Password = "78Annie"

$group = "Users"

$adsi = [ADSI]"WinNT://$env:COMPUTERNAME"

$existing = $adsi.Children | where {$_.SchemaClassName -eq 'user' -and $_.Name -eq $Username }

if ($existing -eq $null) {

   Write-Host "Creating new local user $Username."

   & NET USER $Username $Password /add /y /expires:never

   Write-Host "Adding local user $Username to $group."

   & NET LOCALGROUP $group $Username /add

}

else {

   Write-Host "Setting password for existing local user $Username."

   $existing.SetPassword($Password)

}

Write-Host "Ensuring password for $Username never expires."

& WMIC USERACCOUNT WHERE "Name='$Username'" SET PasswordExpires=FALSE

Solutions

Expert Solution

PS C:\> $Password = Read-Host -AsSecureString
PS C:\> New-LocalUser "AnnieSue" -Password $Password -FullName "Annie Sue" -Description "Adding new local non-admin user." -PasswordNeverExpires $true
Name    Enabled  Description
----    -------  -----------
AnnieSue  True     Adding new local non-admin user.

I think you can do this in easy way, just read password in secured manner by $Password = Read-Host -AsSecureString. This command will read password in the form of asterisk.

After that directly create user using New-LocalUser command as shown above in code snippet.

Below are all the options available with New-LocalUser command:-

New-LocalUser [-AccountExpires <DateTime>] [-AccountNeverExpires] [-Description <String>] [-Disabled] [-FullName <String>] [-Name] <String> -Password <SecureString> [-PasswordNeverExpires] [-UserMayNotChangePassword] [-WhatIf] [-Confirm] [<CommonParameters>]


Related Solutions

Write a Powershell script to create a Windows user account that is a non-admin user. This...
Write a Powershell script to create a Windows user account that is a non-admin user. This assignment assumes that you already have a Windows VM, with the administrator account created.. To complete the assignment, write the PowerShell script, run it on your VM, and submit the script here.
Hello, #1 I am asked to create a Windows user account that is a non-admin user...
Hello, #1 I am asked to create a Windows user account that is a non-admin user using a.ps1 file. At first, I used the New-LocalUser cmdlet, but this cmdlet does not show up as a user on the start menu of my computer and I cannot use it to log in as another user on my computer. So, instead of using that cmdlet, what could I use to create a Windows user account that I can also log into? #2...
Windows PowerShell 1) Write a PowerShell Script to monitor a file for changes. 2) Write a...
Windows PowerShell 1) Write a PowerShell Script to monitor a file for changes. 2) Write a PowerShell Script to create a user account in a specific OU.
Write a PowerShell script which will prompt user to enter the number of the day of...
Write a PowerShell script which will prompt user to enter the number of the day of the week (e.g. 1,2,3,4,5,6,7) and return the day of the week. (e.g. Sunday...etc.) (Hint: Sunday is the 1st day of the week).
Hi. I'm writing a project about overhead lines, and have a question regarding AC and DC...
Hi. I'm writing a project about overhead lines, and have a question regarding AC and DC in overhead line. Is there any different in the electric field if you look at AC and DC And do you know anything about the simulation program FEMM Hope you can help me, good day
creating a bash script called guessingGame.sh The guessingGame.sh should generate a random number that the user...
creating a bash script called guessingGame.sh The guessingGame.sh should generate a random number that the user has to guess. Each time after a guess input the program will let the user know if their guess is too high or too low until they guess the right number. The script should keep a count of how many guesses have currently been made. If the user the has made five guesses without guessing correctly the program should give a hint and let...
Windows PowerShell Scripting Please answer using a switch statement. I have asked this question already and...
Windows PowerShell Scripting Please answer using a switch statement. I have asked this question already and it was wrong. Thank you and I will upvote. 1) Write a PowerShell script that displays the total count of each type (dll, txt, log) of files in c:\windows using switch statement.
write a script in ruby to automate user account creation in Linux using a CSV file....
write a script in ruby to automate user account creation in Linux using a CSV file. Username is a combination of first initial last initial of the first name and last name in the last name followed by the first initial last initial(Ex. Sam Smith = smithsm). If two or more employees had the same first and last name appends a number to the end of the username. After accounts are created write the first and last names along with...
Question #11. Write a script that uses the menu function to prompt the user to select...
Question #11. Write a script that uses the menu function to prompt the user to select the current day of the week. Then use the menu function to prompt the user to select their favorite day of the week. Print a message telling the user how many days it will be until their favorite day. The output must include the current day, the favorite day, and the number of days until the favorite day. Debug your programming by running it...
Hi,This question is very important and I need a quick your answer.Br//Ha a. Consider an economy...
Hi,This question is very important and I need a quick your answer.Br//Ha a. Consider an economy that is characterised by the following Phillips curve: ? = ?- − ?(? − ?e ), where ? is unemployment, ?- is the natural rate of unemployment, ? is inflation, ?e is inflation expectations and ? > 0 is a parameter. Suppose that the loss function of the central bank is given by: ?(?, ?) = ? + 3/2 ?2, Agents are assumed to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT