Solved

file vs variable in powershell

Posted on 2014-02-23
7
179 Views
Last Modified: 2014-02-26
How come I have to use the split option when dealing a list in variable but not when pulling from a file with gc? Both look identical to me but clear

$servers='aaa
bbb
ccc'
$a=0;
$servers -split '\n'|%{
write-host $a - $_;  
$a++
}

Open in new window


gc test1.txt
aaa
bbb
ccc
gc test1.txt|%{
write-host $a - $_;  
$a++
}

Open in new window

0
Comment
Question by:Mazdajai
  • 4
  • 2
7 Comments
 
LVL 28

Expert Comment

by:becraig
Comment Utility
Try explicitly declaring the array

$servers= @('aaa','bbb','ccc')
0
 
LVL 21

Author Comment

by:Mazdajai
Comment Utility
>>Try explicitly declaring the array

Typing 100+ items with single quote and comma would be working backward.

I would like the list in original format for portability purpose.
0
 
LVL 28

Expert Comment

by:becraig
Comment Utility
Is there some reason you have to type these in ?
What are you trying to do and why can't your list be populated dynamically ?
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 21

Author Comment

by:Mazdajai
Comment Utility
Most list I work with is often one per each line, therefore declaring an array would not work.

I can put them in a file and use gc but for some something quick I would like to do it in one shot. It came across that the split option is needed for variable and there I would like to know, out of curiosity.
0
 
LVL 28

Expert Comment

by:becraig
Comment Utility
If you've got an array there must be a way to determine an element

A comma works I used the quote to let powershell know its a string

You don't need to use split if a known delimiter is used with each element.
0
 
LVL 39

Accepted Solution

by:
footech earned 200 total points
Comment Utility
In your first example, $servers is just a string that happens to span multiple lines.  There's nothing that says a string can only be one line.
On the other hand, gc produces an array from a multi-line file.
0
 
LVL 28

Assisted Solution

by:becraig
becraig earned 200 total points
Comment Utility
The quick answer is your example wraps all the entries (carriage return or not) as a single string.
Eg: $var = "the lazy brown Fox"
Is the same as
$var = "the
Lazy
Brown
Fox"
More information on arrays
 http://ss64.com/ps/syntax-arrays.html
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

728 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

10 Experts available now in Live!

Get 1:1 Help Now