Link to home
Start Free TrialLog in
Avatar of patron
patronFlag for India

asked on

Powershell script to create AD User

>Should contain only lower-case characters and numbers and the only special characters allowed are "_" and ".". So: ("[a-z0-9_.]")
> If there is no "." in the username, the script should ask for confirmation before creation of the user. The message should be:
"The username does not contain a dot, are you sure you want to continue?"
>Maximum amount of characters = 31
> Minimum amount of characters = 3
 and how to create user in specific CN [Built in OU] with Manuel entry?
Avatar of Prashant Sabnekar
Prashant Sabnekar
Flag of India image

Import-Module ActiveDirectory
New-ADUser `
 -Name "TestUser" `
 -Path  "OU=TestOU,DC=TestDomain,DC=Local" `
 -SamAccountName  "TestUser" `
 -DisplayName "Test User" `
 -AccountPassword (ConvertTo-SecureString "MyPassword123" -AsPlainText -Force) `
 -ChangePasswordAtLogon $true  `
 -Enabled $true
Add-ADGroupMember "Domain Admins" "TestUser";
Try this script
$Regex1 = "^[a-z0-9_.]{3,31}$"

$username = "dee_."

if($username -match $Regex1)
{
    if($username -notmatch ".")
    {
        Write-host "Username doesnot contains a ., do you want to continue? (Default is No)" -ForegroundColor Yellow 
        $Readhost = Read-Host " ( y / n ) "

        Switch ($ReadHost) 
         { 
           Y {<#your logic#>} 
           N {Write-Host "No, creating user $username failed"} 
           Default {Write-Host "Default, creating user $username failed" } 
         } 
    }

}

Open in new window

SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of patron

ASKER

Then it should  not create user and ask to give name in valid manner like above
Avatar of patron

ASKER

user name should be abc_xyz or abc.xyz or if none from both it should ask before creating any user like admin [without.,/]
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of patron

ASKER

currently i am using like..

$validUser ="([a-z]){3}[_]\d{4}"
If ($SamAccountName –notmatch $validUser) {
  Write-Error "User name is not in valid format. Please enter user name in followin format `r`n ABC_1234"

pls help to update this according to need in question
You really should be able to do that yourself. What do you have issues with exactly? The pattern you use in your last comment and the one you showed in the origirnal question are too different to tell the desired rule.
Avatar of patron

ASKER

Thanks a lot for all your support on this
Avatar of patron

ASKER

Thanks