I’m creating a PowerShell script to create a unique ID based off of some AD info. Here is what I have so far.
Import-Module -Name ActiveDirectory
$username = Read-Host -Prompt ("Please Enter the Username")
$startdate = Read-Host -Prompt ("Please Enter the user's Start Date (Format: Feb 2022 = 022022)")
Basically I want to create another variable called $UserID that will concatenate the first letter of the given name for the Active Directory user + first letter of the surname for the Active Directory user but lowercase + $startdate. So for example, if I input jdoe for username for John Doe in AD and the startdate is 022022, UserID should be Jd022022.
Is there a way to concatenate this in PowerShell?
>Solution :
You can use .tolower() and .Substring() methods to do this.
Import-Module -Name ActiveDirectory
$username = Read-Host -Prompt ('Please Enter the Username')
$startdate = Read-Host -Prompt ("Please Enter the user's Start Date (Format: Feb 2022 = 022022)")
$user = Get-ADUser -Identity $username
$finalvariable = $user.GivenName.Substring(0,1) + $user.Surname.Substring(0,1).tolower() + $startdate
$finalvariable