I'm working on a PS script that will allow a user to paste a bunch of emails on the command line, then the script will parse each one out and run another function on them.
I've looked around for a solution as well as played around in VS Code, but nothing seems to work how I would like it.
The format of the pasted emails by the user will be as follows, copied from a txt file:
[email protected]
[email protected]
[email protected]
Each email separated by a newline.
Using Read-Host, if I paste multiple lines, it just takes the first line, runs whatever function I have on it, then errors out on the next lines.
Essentially I'd like input/output to look like this:
Paste emails:
[email protected]
[email protected]
[email protected]
Operation was performed on email [email protected]
Operation was performed on email [email protected]
Operation was performed on email [email protected]
Any help would be much appreciated. Thank you!
Read-Host
inherently supports only one line of input, so you have the following options:Make your users copy and paste a single-line list of space-separated email addresses (e.g.,
[email protected] [email protected] ...
)Read-Host
return value into an array of addresses with$addresses = -split (Read-Host ...)
Use a GUI-based prompting mechanism that accepts multi-line input - see sample code below.
Alternatively:
Make the user specify the path to the file containing the email addresses, which you can then read with
Get-Content
.If you don't mind having to press Enter twice after typing only one address or pasting one or multiple ones without a trailing newline, consider js2010's simple loop-based alternative.
Using WinForms to create a multi-line input box:
[Microsoft.VisualBasic.Interaction]::InputBox()
- see this answer.The following creates a sample dialog of fixed size with a multi-line textbox and OK and Cancel buttons (PSv5+, but could be adapted to earlier versions):