Jay Smith
Jay Smith

Reputation: 21

Powershell: Create users from file and create a directory on share

Hello I am attempting to setup a script that imports a txt file of users, separates the firstname/last name, then creates the user in an OU. This works however I attempted to add another command after it but still in the loop that creates a directory on the share since when i tried to tried to set the home directory for the user in the command, the home directory was set but the directory was never created. Therefore I am attempting to add the create directory to this command but i get an error:

Get-Content C:\Users\test.user\Desktop\name.txt | ForEach-Object {
    $Split = $_.Split(" ");
    $given=$Split[0];
    $sur=$Split[1]; 
    $uname=$given + "." + $sur; 
    New-ADUser -GivenName $given -Surname $sur -Name ($given + " " + $sur) `
      -UserPrincipalName (($uname + "@" + "$env:userdnsdomain")).ToLower() `
      -path "OU=testou, DC=testdomain, DC=local" -SamAccountName ($uname).ToLower() `
      -AccountPassword (ConvertTo-SecureString -AsPlainText "pass-word1" `
      -Force) -Enabled $true -ChangePasswordAtLogon $true `
      -Verbose New-Item -Path "\\testdomain.local\userdata\test users\$uname" -ItemType "directory"
}

New-ADUser : Cannot bind parameter because parameter 'Path' is specified more than once. To provide multiple values to parameters that can accept multiple values, use the array syntax. For example, "-parameter value1,value2,value3". At line:1 char:509

I am unsure how to do it like how it recommends as i am new to scripting. How could add the New-Item -Path command to the command so it creates the directory on the share so i can then set user home directories to it. I may then try to implement setting the home directory into this command. I tried to remove -Path since you can create directories without it but then it gave an error: New-ADUser : A positional parameter cannot be found that accepts argument 'New-Item'. At line:1 char:161

Any assistance will be greatly appreciated

Upvotes: 0

Views: 468

Answers (1)

Jim
Jim

Reputation: 18863

While formatting your code for you, the issue is you're missing a ;. Here is the updated version. Next time try not to one-liner it, it makes for a hell of a time to figure out what's going on.

Get-Content C:\Users\test.user\Desktop\name.txt | ForEach-Object {
    $Split = $_.Split(" ");
    $given=$Split[0];
    $sur=$Split[1]; 
    $uname=$given + "." + $sur; 
    New-ADUser -GivenName $given -Surname $sur -Name ($given + " " + $sur) `
      -UserPrincipalName (($uname + "@" + "$env:userdnsdomain")).ToLower() `
      -path "OU=testou, DC=testdomain, DC=local" -SamAccountName ($uname).ToLower() `
      -AccountPassword (ConvertTo-SecureString -AsPlainText "pass-word1" `
      -Force) -Enabled $true -ChangePasswordAtLogon $true `
      -Verbose -WhatIf # Semi-colon here if you want it a one-liner, or new line for readability
    New-Item -Path "\\testdomain.local\userdata\test users\$uname" -ItemType "directory" -WhatIf
}

The New-Item needs to be on a new line, or a semi-colon needs to be after -Verbose.

EDIT

Added in -WhatIf per postanote's comments, and I agree.

Upvotes: 0

Related Questions