# How to pipe output results to variable

**URL:** <https://forums.powershell.org/t/how-to-pipe-output-results-to-variable/13487>\
**Category:** PowerShell Help\
**Created:** [December 1, 2019, 3:26pm UTC](https://forums.powershell.org/t/how-to-pipe-output-results-to-variable/13487 "2019-12-01T15:26:04Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![trgrassijr](https://avatars.discourse-cdn.com/v4/letter/t/fbc32d/32.png) [@trgrassijr](https://forums.powershell.org/u/trgrassijr)\
**Post date:** [December 1, 2019, 3:26pm UTC](https://forums.powershell.org/t/how-to-pipe-output-results-to-variable/13487/1 "2019-12-01T15:26:04Z")

</div>

Need output results piped out to file

[pre]

PS C:\WINDOWS\system32\> $results = invoke-command -computer TGCS001-2012R2 -scriptblock {reg delete “HKEY\_LOCAL\_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager” /v PendingFileRenameOperations /f}

PS C:\WINDOWS\system32\> $results = invoke-command -computer TGCS001-2012R2 -scriptblock {(Get-ItemProperty -Path ‘Registry::HKEY\_LOCAL\_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager’ -Name PendingFileRenameOperations ).PendingFileRenameOperations}

Property PendingFileRenameOperations does not exist at path HKEY\_LOCAL\_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager.

- CategoryInfo : InvalidArgument: (PendingFileRenameOperations:String) [Get-ItemProperty], PSArgumentException
- FullyQualifiedErrorId : System.Management.Automation.PSArgumentException,Microsoft.PowerShell.Commands.GetItemPropertyCommand
- PSComputerName : TGCS001-2012R2

&nbsp;

PS C:\WINDOWS\system32\> $results  
The operation completed successfully.

[/pre]

As you can see the first command places results in $Results. The second command places results to console

How do I get the results to be added to $results

I tried +=

&nbsp;

Any ideas?

&nbsp;

Thank you

&nbsp;

Tom

&nbsp;

&nbsp;

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![js2010](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@js2010](https://forums.powershell.org/u/js2010)\
**Post date:** [December 1, 2019, 5:59pm UTC](https://forums.powershell.org/t/how-to-pipe-output-results-to-variable/13487/2 "2019-12-01T17:59:14Z")

</div>

After the first command, $result isn’t an array, so you can’t add to it that way.

```
[array]$results +=
```

---

<div class="post-metadata">

**Author:** ![trgrassijr](https://avatars.discourse-cdn.com/v4/letter/t/fbc32d/32.png) [@trgrassijr](https://forums.powershell.org/u/trgrassijr)\
**Post date:** [December 1, 2019, 6:28pm UTC](https://forums.powershell.org/t/how-to-pipe-output-results-to-variable/13487/3 "2019-12-01T18:28:50Z")

</div>

JS

[pre]

try  
{  
[array] $results = invoke-command -computer TGCS001-2012R2 -scriptblock {reg delete “HKEY\_LOCAL\_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager” /v PendingFileRenameOperations /f}  
}  
catch  
{  
[array] $results += invoke-command -computer TGCS001-2012R2 -scriptblock {(Get-ItemProperty -Path ‘Registry::HKEY\_LOCAL\_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager’ -Name PendingFileRenameOperations ).PendingFileRenameOperations}  
}

Added the array and when it errors I do not get anything in $results

I added the try catch

If line 1 has a value I need line 2 to run if line one is not present no need to run line two.

&nbsp;

Any ideas?

&nbsp;

&nbsp;

&nbsp;

[/pre]

---

<div class="post-metadata">

**Author:** ![js2010](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@js2010](https://forums.powershell.org/u/js2010)\
**Post date:** [December 1, 2019, 6:31pm UTC](https://forums.powershell.org/t/how-to-pipe-output-results-to-variable/13487/4 "2019-12-01T18:31:17Z")

</div>

Well, the error says pendingfilerenameoperations does not exist.

---

<div class="post-metadata">

**Author:** ![trgrassijr](https://avatars.discourse-cdn.com/v4/letter/t/fbc32d/32.png) [@trgrassijr](https://forums.powershell.org/u/trgrassijr)\
**Post date:** [December 1, 2019, 6:55pm UTC](https://forums.powershell.org/t/how-to-pipe-output-results-to-variable/13487/5 "2019-12-01T18:55:55Z")

</div>

Yes so how do I code that

&nbsp;

Any ideas?

---

<div class="post-metadata">

**Author:** ![j2sankha](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@j2sankha](https://forums.powershell.org/u/j2sankha)\
**Post date:** [December 2, 2019, 7:18am UTC](https://forums.powershell.org/t/how-to-pipe-output-results-to-variable/13487/6 "2019-12-02T07:18:36Z")

</div>

You have to handle the error. First of all, writing through mobile, so unable to design well. But can guide you the concept.

[pre]

try {

$result += 1st statement -ea stop

$result += 2nd statement -ea stop

}

Catch { $result += $\_.exception.message}

[/pre]

So, successful statement saves its output through try block and failed statement will do the same through catch block.

Let me know your result.

---

<div class="post-metadata">

**Author:** ![trgrassijr](https://avatars.discourse-cdn.com/v4/letter/t/fbc32d/32.png) [@trgrassijr](https://forums.powershell.org/u/trgrassijr)\
**Post date:** [December 2, 2019, 9:40am UTC](https://forums.powershell.org/t/how-to-pipe-output-results-to-variable/13487/7 "2019-12-02T09:40:57Z")

</div>

Sankhadip Roy

Thank you that seems to work out

How can I add the computer name to the $Results I tried this with no luck

&nbsp;

[pre]

try  
{  
$results = invoke-command -computer TGCS001-2012R2 -scriptblock {reg delete “HKEY\_LOCAL\_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager” /v PendingFileRenameOperations /f} -ea Stop

$results += invoke-command -computer TGCS001-2012R2 -scriptblock {(Get-ItemProperty -Path ‘Registry::HKEY\_LOCAL\_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager’ -Name PendingFileRenameOperations ).PendingFileRenameOperations} -ea Stop

$Results += ‘Server TGCS001-2012R2’  
}  
catch { $Results += $\_.exception.message}

[/pre]

PS C:\WINDOWS\system32\> $results  
ERROR: The system was unable to find the specified registry key or value.

&nbsp;

Would like $results to show

Server = TGCS001-2012R2  
ERROR: The system was unable to find the specified registry key or value.

Any ideas?

Thanks

Tom

&nbsp;

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![trgrassijr](https://avatars.discourse-cdn.com/v4/letter/t/fbc32d/32.png) [@trgrassijr](https://forums.powershell.org/u/trgrassijr)\
**Post date:** [December 2, 2019, 10:45am UTC](https://forums.powershell.org/t/how-to-pipe-output-results-to-variable/13487/8 "2019-12-02T10:45:42Z")

</div>

Guys

Got a little further

[pre]

$Results += ‘Server TGCS001-2012R2’ | out-string  
try  
{  
$results += invoke-command -computer TGCS001-2012R2 -scriptblock {reg delete “HKEY\_LOCAL\_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager” /v PendingFileRenameOperations /f} -ea Stop

$results += invoke-command -computer TGCS001-2012R2 -scriptblock {(Get-ItemProperty -Path ‘Registry::HKEY\_LOCAL\_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager’ -Name PendingFileRenameOperations ).PendingFileRenameOperations} -ea Stop

}  
catch { $Results += $\_.exception.message}

$Results += ‘Server TGCS002-2016’ | out-string  
try  
{  
$results += invoke-command -computer [TGCS002-2016.home.tgcsnet.com](http://TGCS002-2016.home.tgcsnet.com) -scriptblock {reg delete “HKEY\_LOCAL\_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager” /v PendingFileRenameOperations /f} -ea Stop

$results += invoke-command -computer [TGCS002-2016.home.tgcsnet.com](http://TGCS002-2016.home.tgcsnet.com) -scriptblock {(Get-ItemProperty -Path ‘Registry::HKEY\_LOCAL\_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager’ -Name PendingFileRenameOperations ).PendingFileRenameOperations} -ea Stop

}  
catch { $Results += $\_.exception.message}

[/pre]

&nbsp;

The output looks like this

PS C:\WINDOWS\system32\> $results  
Server TGCS001-2012R2  
ERROR: The system was unable to find the specified registry key or value.Server TGCS002-2016  
ERROR: The system was unable to find the specified registry key or value.

&nbsp;

How can I get the second, third forth etc computers on a separate line

Server TGCS001-2012R2  
ERROR: The system was unable to find the specified registry key or value.

Server TGCS002-2016  
ERROR: The system was unable to find the specified registry key or value.

A blank line between each computer would be nice also.

&nbsp;

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![trgrassijr](https://avatars.discourse-cdn.com/v4/letter/t/fbc32d/32.png) [@trgrassijr](https://forums.powershell.org/u/trgrassijr)\
**Post date:** [December 2, 2019, 2:04pm UTC](https://forums.powershell.org/t/how-to-pipe-output-results-to-variable/13487/9 "2019-12-02T14:04:56Z")

</div>

Mark this as resolved

I figured it out.

---

<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:31pm UTC](https://forums.powershell.org/t/how-to-pipe-output-results-to-variable/13487/10 "2024-05-16T20:31:05Z")

</div>


