Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to concatenate certain strings in PowerShell?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading