# Spliting a string....Newbie

**URL:** <https://forums.powershell.org/t/spliting-a-string-newbie/6197>\
**Category:** PowerShell Help\
**Created:** [April 12, 2016, 6:02am UTC](https://forums.powershell.org/t/spliting-a-string-newbie/6197 "2016-04-12T06:02:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![scott-windmiller](https://avatars.discourse-cdn.com/v4/letter/s/c4cdca/32.png) [@scott-windmiller](https://forums.powershell.org/u/scott-windmiller)\
**Post date:** [April 12, 2016, 6:02am UTC](https://forums.powershell.org/t/spliting-a-string-newbie/6197/1 "2016-04-12T06:02:23Z")

</div>

Sorry but I am just getting into Powershell so my knowledge is limited.  
I have the following script:  
Add-PSSnapin Quest.ActiveRoles.ADManagement  
$PCS = Get-QADComputer -ManagedBy (Get-QADUser swindmiller) | Select-Object Description

Which gives me an output for $PCS of:  
Model / Serial / Building  
Model / Serial / Building  
Model / Serial / Building

I want to reference each Model, Serial, Building, with something like $PCS[0],$PCS[1],$PCS[2]. I assume I would have to use some sort of foreach command for each line but I am getting tripped up on the split command.

I keep getting:  
Method invocation failed because [Selected.Quest.ActiveRoles.ArsPowerShellSnapIn.Data.ArsComputerObject] does not contain a method named ‘Split’.

Can someone give me a hand and possibly an example?

Thanks,  
Scott

---

<div class="post-metadata">

**Author:** ![bobmccoy](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/bobmccoy/32/650_2.png) [@bobmccoy](https://forums.powershell.org/u/bobmccoy)\
**Post date:** [April 12, 2016, 6:13am UTC](https://forums.powershell.org/t/spliting-a-string-newbie/6197/2 "2016-04-12T06:13:17Z")

</div>

- Are there literal spaces on either side of the slashes?
- It would be better if you could post real world data (sanitized as necessary) rather than the made up data above.
- Also itls not clear from above what "$PCS[0],$PCS[1],$PCS[2]:" means.
- And is there a reason why you are using the Quest cmdlets instead of the native AD cmdlets?

---

<div class="post-metadata">

**Author:** ![jason-yoder](https://avatars.discourse-cdn.com/v4/letter/j/f0a364/32.png) [@jason-yoder](https://forums.powershell.org/u/jason-yoder)\
**Post date:** [April 12, 2016, 6:15am UTC](https://forums.powershell.org/t/spliting-a-string-newbie/6197/3 "2016-04-12T06:15:34Z")

</div>

Scott, I do not have access to the Quest CMDLETs, but I have an idea for you. Pipe your output to Get-Member. If the TYPENAME is System.String, the you can try to split your output using “`n” That is a back tick in front of the n, not a single quote.  
First save all of the output into a single variable. Let’s call this variable $Data.  
Next, Split the data: $Split = $Data.Split(“`n”)  
Not access the data. $Split[0]

---

<div class="post-metadata">

**Author:** ![bobmccoy](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/bobmccoy/32/650_2.png) [@bobmccoy](https://forums.powershell.org/u/bobmccoy)\
**Post date:** [April 12, 2016, 6:38am UTC](https://forums.powershell.org/t/spliting-a-string-newbie/6197/4 "2016-04-12T06:38:27Z")

</div>

I see that the native cmdlets don’t easily offer up -ManagedBy. You could do something like this …

```
Add-PSSnapin -Name Quest.ActiveRoles.ADManagement
$mgrName = "swindmiller"
$mgrDN = (Get-QADUser ).DN
$computers = Get-QADComputer -ManagedBy $mgrDN
$results = foreach ($computer in $computers)
{
    $model, $serial, $building = $computer.Description -split "\s*/\s*"
    [PSCustomObject]@{
        Computer = $computer.ComputerName.trimend('$')
        Manager = $mgrName
        Model = $model
        Serial = $serial
        Building = $building
    }
}
# Sample outputs - pick one or more, your choice
$results
$results | Format-Table -AutoSize
$results | Out-GridView
$results | Export-Csv -Path .\foo.csv -NoTypeInformation -Encoding ASCII
$results | Out-File -FilePath .\foo.txt -Encoding ASCII
$results | Export-Clixml -Path .\foo.xml -Encoding ASCII
```

---

<div class="post-metadata">

**Author:** ![scott-windmiller](https://avatars.discourse-cdn.com/v4/letter/s/c4cdca/32.png) [@scott-windmiller](https://forums.powershell.org/u/scott-windmiller)\
**Post date:** [April 12, 2016, 10:09am UTC](https://forums.powershell.org/t/spliting-a-string-newbie/6197/5 "2016-04-12T10:09:29Z")

</div>

Thanks so much Bob McCoy! I am learning but was totally thinking about it the wrong way. Thanks!  
I was then able to use $results | Select-Object Computer,Model at the end.  
This worked perfectly.

The only change I had to make was to the 3rd line:  
$mgrDN = (Get-QADUser ).DN

to

$mgrDN = (Get-QADUser $mgrName).DN

Scott

---

<div class="post-metadata">

**Author:** ![bobmccoy](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/bobmccoy/32/650_2.png) [@bobmccoy](https://forums.powershell.org/u/bobmccoy)\
**Post date:** [April 12, 2016, 8:58pm UTC](https://forums.powershell.org/t/spliting-a-string-newbie/6197/6 "2016-04-12T20:58:22Z")

</div>

Yeah, sorry. That was an edit error when I was removing real world data from the posted script.

As with just about anything in PowerShell you will find there are multiple ways to get it done. I prefer a collection of objects. That gives me maximum flexibility in my output options at the end.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:41pm UTC](https://forums.powershell.org/t/spliting-a-string-newbie/6197/7 "2024-05-16T20:41:34Z")

</div>

Closed on May 16, 2024, 8:41pm UTC


