Solved

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

Posted on 2013-06-03
2
855 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 500 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Powershell 2.0/3.0 2 42
Powercli validate module 6 49
invoke-sqlcmd help 5 33
List all groups and nested groups in AD with powershell 4 32
Synchronize a new Active Directory domain with an existing Office 365 tenant
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
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ā€¦

734 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