Posted On October 12, 2022

PowerShell: How to Convert Multi-Line Texts Into an Array of Strings

kimconnect 0 comments
blog.KimConnect.com >> Codes >> PowerShell: How to Convert Multi-Line Texts Into an Array of Strings

Significance: we often run into situations where a list of items (such as usernames, computernames, etc.) that are needed to be actioned within a PowerShell script. Instead of manually creating double quotes around each string, we could quickly paste them into a text file or a text variable signified with the @ symbol as demonstrated below

Example 1:

# Paste a list of names here
# Important: there could spaces on each line
$usersList = @'
username1
username2
username3
'@

$userNames = @($usersList -split "`n")|%{$_.Trim()}

Example 2:

# Paste a list of names here
# Important: there must be no spaces nor extra characters between each name
$computersList = @'
computername1
computername2
computername3
'@

$computerNames = @($computersList -split "`n" -replace "\..*$")

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

Basic HTML and HTML5: Create a Bulleted Unordered List

<h2>CatPhotoApp</h2><main><p>Click here to view more <a href="#">cat photos</a>.</p><a href="#"><img src="https://bit.ly/fcc-relaxing-cat" alt="A cute orange cat lying…

SQL: Use PowerShell to Generate a Database Object From Another Object

Have you ever wondered about the prospect of automating T-SQL executions on Windows? As your…

Linux: How To Use Dig

Checking Name Server(s) kim@kim-linux:~$ dig @8.8.8.8 microsoft.com ; <<>> DiG 9.16.1-Ubuntu <<>> @8.8.8.8 microsoft.com ;…