Solved

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

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article will help you understand what HashTables are and how to use them in PowerShell.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
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ā€¦
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

696 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