Solved

file vs variable in powershell

Posted on 2014-02-23
7
193 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
[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
  • 4
  • 2
7 Comments
 
LVL 29

Expert Comment

by:becraig
ID: 39881588
Try explicitly declaring the array

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

Author Comment

by:Mazdajai
ID: 39881592
>>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 29

Expert Comment

by:becraig
ID: 39881599
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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 21

Author Comment

by:Mazdajai
ID: 39881611
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 29

Expert Comment

by:becraig
ID: 39881640
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 40

Accepted Solution

by:
footech earned 200 total points
ID: 39881673
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 29

Assisted Solution

by:becraig
becraig earned 200 total points
ID: 39881701
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
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…

739 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