?
Solved

How can I configure a read-host statement in powershell to accept input that contains spaces

Posted on 2013-06-03
2
Medium Priority
?
973 Views
Last Modified: 2013-06-04
Hello Powershell Experts,

I'm working on a script that will update some AD information for users.  I identify the user that for which the script will run with a read-host statement.  The read-host statement is currently configured as:

read-host "Input the username"

I would input nickd (as our usernames are firstname + first initial of the last name") and everything works fine.

I want to change the read-host statement to:
read-host "Input the first and last name"

How can I do this?  The space between the firstname and the lastname are causing issues.

Thanks in advance.
Nick
0
Comment
Question by:ndalmolin_13
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 17

Accepted Solution

by:
Learnctx earned 2000 total points
ID: 39217712
You can use a regular expression or split the string to try do what you want to achieve. I prefer RegEx so I'll do that.

read-host "Input the username" | ForEach-Object {$_ -replace "(?<=^[a-z'-]+)\s|(?<=\s[a-z])[\w\W]+$"}

Open in new window

Should be able to handle names like:

John Smith
Martin Luther King
Martin Luther King, Jr.
John Wayne-Smith
John O'Hara
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 39218257
In which way is that an issue? The whole input is considered to be a single string.
Do you need to split it into first and last name? Without any check, -split is the simplest way to do so:
$first, $last = (read-host "Input the first and last name") -split " "

Open in new window

0

Featured Post

Ransomware Attacks Keeping You Up at Night?

Will your organization be ransomware's next victim?  The good news is that these attacks are predicable and therefore preventable. Learn more about how you can  stop a ransomware attacks before encryption takes place with our Ransomware Prevention Kit!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question