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

Why accessing this json sub elements by index raises this error?
How to apply WorksheetFunction.Average to a dynamic array in VBA
Getting array length in mongodb?
using vb.net and LibTiff to convert large tiff images
Storing the output of spap2 into an array then looping and graphing that output
Using intermediate variable to work with array (reference type)
Swift Creating tableView From JSON
How how convert normal string into multi-line string
How to convert a double to double array?
Use one index from array
ko.observableArray not UI not UPdating.
Creating collection of different enums
Determine Winter or Summertime due to month (UTC Timezone) [duplicate]
UDF if value is in array - Type Mismatch from a prior answer - IsInArray
matrix and vector to 3-d array R
Angular, merge three arrays to one where id match

Categories

HOME
pug
mql4
meshlab
mapping
decorator
windows-store-apps
javamail
jrebel
docker-windows
vsftpd
slim-3
octopus-deploy
diagram
cs-cart
quartus
android-fragmentactivity
apply
thingsboard
jquery-scrollify
delete-file
fetch-api
pentaho-report-designer
frp
reply
google-api-dotnet-client
blackboard
pep8-assembly
piecewise
rhomobile
pace
knockout-3.0
service-fabric-stateful
paho
ivona
xmgrace
ogre3d
chrome-remote-desktop
volume
fontconfig
search-box
lotus
pdfkit
createobject
libtiff.net
master
freetype2
unsatisfiedlinkerror
kendo-combobox
faraday
codesign
inet
mirrorlink
selendroid
shift-jis
pl-i
cpu-speed
hidden-field
cg
jstat
sid
emma
adaptive-compression
surrogate-key
boost-test
colt
data-generation
visualstatemanager
help-viewer
zipalign
broadcasting
crystal-reports-10
edit-in-place
arangodb-php
cling
document-database
sslexception
trimming
installshield-2009
dynamic-binding
fieldset
vt100
mozart-mvc
git-filter-branch
returnurl
qsqltablemodel
deploying
ajax.beginform
nemerle
hardware-acceleration
surefire
chdatastructures
winverifytrust
graniteds
windows-controls
virtualquery

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