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

Pascal Arrays Can't figure out how to print info from arrays
Arrays with negative indices in FreePascal
MongoError: exception: Cannot apply the positional operator without a corresponding query field containing an array
Delete sheets if their names belong to an array
Delphi - Re-split an array of string?
Split a String array after X number of comma
Searching with filter, not working correctly - Parse, Swift
addToSet if not already present
How to convert group of numbers in text file into matrix array
How to create a reverse sort on an Excel array
Deeply-placed array contents were missing
Difference between [String] and Array<String>
Split a numpy array by a key array
“no such instruction error” when assembling an array declaration:
VBA Add All Range Variables to an Array
Check for integer in string array

Categories

HOME
batch-processing
fparsec
jbehave
youtube-livestreaming-api
matplotlib
automated-tests
adsense
data-science-experience
sudo
vuex
jboss-eap-7
jboss7.x
game-maker-studio-1.4
phoenix
multiple-monitors
medium-editor
arm-template
atlassian-plugin-sdk
eclipselink
clover
multiple-columns
jni
jconsole
plsqldeveloper
esql
cruisecontrol.net
autolayout
hhvm
opencart2.3
vcard
superscript
xor
ioc-container
m2e
hotmail
nlb
visualstudio.testtools
apiary
gd
multi-agent
component-pascal
garrys-mod
system.web.ui.webcontrols
debugdiag
mongoose-populate
multiple-file-upload
mercurial-hook
multilingual-app-toolkit
gmsmapview
test-class
anythingslider
drf-nested-routers
insert-into
textkit
viper-architecture
digits
debian-based
pylearn
google-earth-plugin
errorprovider
grunt-contrib-concat
mfmailcomposeviewcontroll
pic24
sanitization
java.util.date
convex-polygon
installshield-2009
apache2.2
fraud-prevention
crocodoc
sequelpro
pstree
xmlslurper
windows-phone-7-emulator
coff
dashcode
j-interop
unreachable-code
chdatastructures
grooveshark
rtsp-client
winsnmp

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App