arrays


create array from string or variable


I'm trying to find the best way using PowerShell to create an array from the output of a variable.
I have the following code:
$ListNames = & "listNames.cmd" | Out-String
Which gives me the following output:
(c) 2016 - 2017 Use of this software is subject to license restrictions
::Microsoft Windows 7
::Microsoft Windows 7::name1
::Microsoft Windows 7::name2
::Microsoft Windows 7::name3
::Microsoft Windows 7::name4
::Microsoft Windows 7::name5
::Microsoft Windows 7::name6
::Microsoft Windows 7::name7
I need in an array everything from name1 thru name7.
I attempted without success trying to convert it to a CSV using delimiter :, tried creating a PSObject, and a few other ways that didn't get me the result I was looking for.
Any help or guidance would be greatly appreciated!

I would probably do something like this:
Remove the Out-String. You want to process the output line-by-line anyway, so merging the lines would only require you to split them again later.
Limit the output to relevant lines by piping it through a Where-Object filter (find or findstr would probably work too).
Split each remaining line at :: and pick the last field from each resulting array.
& ".\listNames.cmd" | Where-Object {
$_ -like '::*::*'
} | ForEach-Object {
($_ -split '::')[-1]
}
Another option would be a regular expression match as #TheMadTechnician suggested:
& ".\listNames.cmd" | Where-Object {
$_ -match '^::.+?::(.+)'
} | ForEach-Object {
$matches[1]
}


Related Links

Subtracting from each element in array in python
C# Problems importing a file to an array and put each character in it's own index
How to identify the position of a sequence of elements in an array in bash
Combine values of two arrays to form key and values to hash in ruby [duplicate]
Create array with key from dict with sorted by dict values
How do I convert every element in an array to its own array?
Adding a list to an array from different file format in perl
How to use Perl `sort` and `pairwise` if I already have variables `$a` and `$b`
Sum dynamic part of array values Excel VBA
Ada How to get input a list of integer from a user and put it into an array
count occurences in for loop
Update / create subdocument in array
Delphi: const list of const objects [duplicate]
Fastest way to search through a CSV and return specific array of values (Ruby)
Excel complicated array
Multiple loops in arrays in VBScript

Categories

HOME
cntk
crate
wmic
system-verilog
blockchain
nuxeo
backup
fparsec
smartphone
parse-server
windows-store-apps
safari
javamail
value
dataframe
file-io
dryioc
data-synchronization
mongodb-query
glpk
typeahead
ada
thumbnails
marathon
kitematic
k2
tfs2013
philips-hue
rhandsontable
nesc
vertex-buffer
autofill
roundcube
protobuf-net
canvasjs
ifstream
node-horseman
rhino
coreclr
sonarqube-scan
linq-to-entities
lexical-analysis
intellilock
dart-pub
constants
homekit
email-parsing
tableau-server
floor
recycle-bin
gd
multi-agent
persistent
mcrypt
httplib2
uicollectionviewlayout
redux-router
efxclipse
rsqlite
pdflib
tomee
c#-interactive
slam-algorithm
dbscan
galaxy
lmfit
ibm-data-studio
isml
git-checkout
jazz
es2015
ctrlp
chaining
tcpreplay
dmp
late-static-binding
anjuta
fogbugz-api
nsmatrix
goinstant
ng-hide
dynamic-binding
swrl
vmware-server
qss
xslkey
mvs
dropdownlistfor
git-filter-branch
n-layer
isolatedstorage
hobo
v4l
viewdidload
objectbrowser
querystringparameter
text-size
scraperwiki
phpcrawl
smooth
loadui
hardware-acceleration
clients
internals
virtualquery





Mobile Apps Dev
Database Users
javascript
java
csharp
php
android


MS Developer
developer works
python
ios
c
html
jquery


RDBMS discuss