Summary: Microsoft PowerShell MVP, Sean Kearney, shows how to use Windows PowerShell string techniques to create user names for Active Directory.
Microsoft Scripting Guy, Ed Wilson, is here. It is time for PowerShell in Blueville Part 3 with Honorary Scripting Guy and Windows PowerShell MVP, Sean Kearney. WooHoo…
So where we did go? What did we say?
Oh, right! Mr. Finch was just dancing away!
He had learned from two guys, both ending in Blue,
Two fellows he met called Hugh and called Stu,
Of PowerShell from that Microsoft land,
A beautiful thing, which did gave him a hand.
It unlocked divisions in one line of work,
And exported data with nary a quirk.
So off Mr. Finch did run to the site,
And obtained his user list, not overnight!
Now back in his office, import them he must,
In PowerShell, he was certain this he could trust.
Smart was Mr. Finch, he knew what to do.
He leapt down five floors and consulted a Blue!
With a thud he did land, with thick padded shoe,
Thank goodness he bought them, and so bouncy too.
Leaping down stairs was silly you see,
Unless you had shoes as thick as a tree.
So blinking away and trying to see,
He saw his goal looming, the Blues of Tier three.
He opened the door and saw one alone,
Quickly finish a task on the unified phone.
Smiling away at a table for two,
Was the smartest of all, her name it was Sue.
And before you could say, “Stop that rhyming, please” twice,
She smacked RhymeOMatic with a large bag of rice.
“Hello, Mr. Finch, I’m Sue…Sue Blue.”
Mr. Finch smiled. Somehow he suspected that. “I have a problem but I suspect you’re going to say, ‘With Windows PowerShell, that’s a breeze!’”
“Yes, it probably is, but what do you need to do?”
Mr. Finch showed Sue the exported CSV file from the Quest cmdlets he obtained from the client site. “I cleaned it up so I have only the First and Last names of the users, but I need to import this into an OU I created under the Boston OU called ‘Contoso’. I found a cmdlet called New-QADUser, and most of it makes sense, but I am having problems. First off, where do I start?”
“So what is in your file, Mr. Finch?”
“I have a list of First Names and Last Names of all the staff brought over from Contoso. However, what I would really like to know is…Is there an easy way with Windows PowerShell to build the User IDs in the system? I figured if I could get the new staff in with a simple password, I could update the additional data later. Our UserID is normally a user’s first name followed by their last initial.”
“Absolutely Mr. Finch. For this, we’re going to build a small script to…”
Mr. Finch shuddered. “Scripts. Oh, that’s FAR too difficult for me. I never learned scripting.”
“If you’ll allow me to finish. Let me show you at least how you make that user name in Windows PowerShell. Then we’ll see who can’t script.”
“Let’s show you some basic work with what’s called a variable in Windows PowerShell. If we put a dollar sign ($) before a word in Windows PowerShell, it’s a variable. Windows PowerShell variables can have very descriptive names like $MrFinch or $BlueScreenOfLife or $FriedChicken95BumbleBees. So let us show you how to build a user name. Let us assign a make-believe user to two Windows PowerShell variables.”
$FirstName=’Gazoo’
$LastName=’Blue’
“Let’s imagine our user name was simply the first and last names added together. Within Window PowerShell, if we type the following…”
$FirstName+$Lastname
“…it will show us this as if we added them together:”
GazooBlue
“Now if we want to have Windows PowerShell remember that, we use another variable to remember that.”
$Username=$Firstname+$Lastname
“Now, you try Mr. Finch. Try your name where I used $FirstName and $LastName.”
Mr. Finch tentatively walked up to the keyboard like it was going to bite him. He typed slowly, unsure of what the computer might do to him—like swallow him whole—or worse, work in VisiCalc.
$FirstName=’Mister’
$LastName=’Finch’
$Username=$FirstName+$LastName
“Now watch what happens when I type $Username,” coaxed Sue Blue.
MisterFinch
Mister Finch nearly fell out of his azure blue boxer shorts. It…it just worked! “That wasn’t difficult!” he muttered to himself. “So how could we make that into our standard user name? Is there a way to tell Windows PowerShell to only use the first letter?”
Sue Blue nodded. “Windows PowerShell has built-in features to pull information out exactly the way you want it. We just add ‘substring()’ to the end of the Windows Powershell variable, and we tell it how much we want out.”
“Here’s an example. This will show us all the letters from the word starting at position 0 (zero), which is the first position, and it will return three letters.” Sue Blue keyed into screen as the results returned.
$LastName.substring(0,3)
Fin
“Oh!” burst out Mr. Finch, taking initiative. “So if I only wanted the first character, which would be position 0 and only ONE character it would be:”
$Lastname.substring(0,1)
“Which means…I think it means…to make my user name, I do believe this would work.”
He carefully typed in the console, no longer frightened, but interested and excited.
$Username=$Firstname+$Lastname.substring(0,1)
Mr. Finch nervously watched as he typed in $Username and saw:
MisterF
“WoooHoooooo!!!! Did I just script?”
Sue Blue smiled. “You will if you put that into Notepad and save it with a file name and .PS1 as the extension.”
Quickly Mr. Finch keyed in:
$Firstname=’Mister’
$Lastname=’Finch’
$Username=$Firstname+$Lastname.substring(0,1)
$Username
Then he saved it as NEWUSER.PS1.
“Now Mr. Finch, I believe I shall guide you down to tier 4, where I believe that the next Blue can help you along.”
It wasn’t so hard, he couldn’t believe,
Scripting seemed easy, oh such a reprieve.
He just couldn’t wait to get started again
In moments in minutes with another Blue friend.
That is all for Part 3. Please join us tomorrow as Sean continues life in Blueville.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy