Home > OS >  How to write multiline Scripts in PowerShell ? I tried using Backtick also but didnt work
How to write multiline Scripts in PowerShell ? I tried using Backtick also but didnt work

Time:11-14

$secPassword = Read-Host "Password" -AsSecureString
New-ADUser
    -Name "XYZ ABC"
    -SamAccountName xyz.a
    -UserPrincipalName "[email protected]"
    -AccountPassword $secPassword
    -Path "cn=Users,dc=ntsh,dc=local"
    -Enabled:$true
    -PasswordNeverExpires:$true
    -CannotChangePassword:$true
    -PasswordNotRequired:$false
    -ChangePasswordAtLogon:$false

It's giving CommandNotFoundException Error for all Parameters. What's wrong in my script ?

CodePudding user response:

The simplest way would be to use the CMDLET in a single line, because it treats them as if they were foreign commands, and the second way if you want to leave it curious, would be to add the symbol ` so that the cmdlet detects that it continues in the next line

The first way:

$secPassword = Read-Host "Password" -AsSecureString
New-ADUser -Name "XYZ ABC" -SamAccountName xyz.a -UserPrincipalName "[email protected]" -AccountPassword $secPassword -Path "cn=Users,dc=ntsh,dc=local" -Enabled:$true -PasswordNeverExpires:$true -CannotChangePassword:$true -PasswordNotRequired:$false -ChangePasswordAtLogon:$false

The second way:

$secPassword = Read-Host "Password" -AsSecureString
New-ADUser -Name "XYZ ABC" `
 -SamAccountName xyz.a `
 -UserPrincipalName "[email protected]" `
 -AccountPassword $secPassword `
 -Path "cn=Users,dc=ntsh,dc=local" `
 -Enabled:$true `
 -PasswordNeverExpires:$true `
 -CannotChangePassword:$true `
 -PasswordNotRequired:$false `
 -ChangePasswordAtLogon:$false

CodePudding user response:

IMHO, splatting is the cleanest way to code as per Zett42's suggestion:

$NADUArgs = @{
    Name                  = "XYZ ABC"
    SamAccountName        = "xyz.a"
    UserPrincipalNam      = "[email protected]"
    AccountPassword       = $secPassword
    Path                  = "cn=Users,dc=ntsh,dc=local"
    Enabled               = $true
    PasswordNeverExpires  = $true
    CannotChangePassword  = $true
    PasswordNotRequired   = $false
    ChangePasswordAtLogon = $false
}

New-ADUser @NADUArgs
  • Related