If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.
Thanks for your quick response. Let's say, an input can be determined as
email address if it contains "@" and ".com"
alias if it contains "." like first name.last name
display name if it contains "," like lastname, first name
$Input = Read-Host -Prompt "Input"
Switch -regex ($Input) {
'.+@.+\.com' {$Type = 'Email'; Break}
'.+,\s*.+' {$Type = 'Display'; Break}
'.+\..+' {$Type = 'Alias'; Break}
default {$Type = 'Unknown'; Break}
}
Write-Host "Type: $($Type)"
$UserInput = Read-Host "`tPlease enter the user"
$AttributeInput = Read-Host "`t`Please enter the required attribute (a comma separated list is possible)`r`n`t"
Switch -regex ($UserInput) {
'.+@.+\.com' {$Type = 'Email'; Break}
'.+,\s*.+' {$Type = 'Display'; Break}
'.+\..+' {$Type = 'Alias'; Break}
default {$Type = 'Unknown'; Break}
}
$AttributeList = $AttributeInput.Split(',') | ForEach-Object {$_.Trim()}
Write-Host "Type: $($Type)"
Write-Host "Attributes:"
$AttributeList | ForEach-Object {Write-Host "`t - $($_)"}
Get-Mailbox -Identity $UserInputName | Select-Object $AttributeList
$UserFile = 'C:\Temp\Users.txt'
$OutputFile = 'C:\Temp\Export.csv'
$AttributeInput = Read-Host "`t`Please enter the required attribute(s) (a comma separated list is possible)`r`n`t"
$AttributeList = @($AttributeInput.Split(',') | ForEach-Object {$_.Trim()})
Write-Host "Attributes:"
$AttributeList | ForEach-Object {Write-Host "`t - $($_)"}
Get-Content -Path $UserFile | ForEach-Object {
$Identifier = $_
Write-Host "Processing '$($Identifier)' ..." -NoNewline
Switch -regex ($Identifier) {
'.+@.+\.com' {$Type = 'Email'; Break}
'.+,\s*.+' {$Type = 'Display'; Break}
'.+\..+' {$Type = 'Alias'; Break}
default {$Type = 'Unknown'; Break}
}
Write-Host " type is $($Type)."
Try {
$Mailbox = Get-Mailbox -Identity $Identifier -ErrorAction Stop
} Catch {
$_.Exception.Message | Write-Host -Fore Red
$Mailbox = '' | Select-Object -Property $AttributeList
$AttributeList | ForEach-Object {$Mailbox.$_ = 'n/a'}
}
$Mailbox | Select-Object (@(@{Name='Identifier'; Expression={$Identifier}}) + $AttributeList)
} | Export-Csv -NoTypeInformation -Path $OutputFile
I believe, it has to be Write-Host "$AttributeList" instead of Write-Host "Attributes:" in the script so that is displays the attributes selected by the user.No. That line is just decoration. The attributes identified will be listed below that line, each attribute in its own line, with the next line of code:
$AttributeList | ForEach-Object {Write-Host "`t - $($_)"}
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Have a better answer? Share it in a comment.
Well, for an email address, you can check whether the string contains a "@" and at least one dot behind it.
But how do you define "alias", and how can you differentiate it from from a "display name" in your scenario?