Solved

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

Posted on 2013-06-03
2
661 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
2 Comments
 
LVL 16

Accepted Solution

by:
Learnctx earned 500 total points
Comment Utility
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 68

Expert Comment

by:Qlemo
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article will help you understand what HashTables are and how to use them in PowerShell.
A procedure for exporting installed hotfix details of remote computers using powershell
This video discusses moving either the default database or any database to a new volume.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now