How to capture all output from icacls.exe in PowerShell
<p>We have a script that runs once a week and adjusts all permissions on a specific file share. I mainly use a PowerShell script for this but also execute icacls.exe in some parts. As there are lots of files, errors will occur when running icacls.exe</p>

<p>At the start of my script, I reset all permissions on each customer folder and after that on all its childitems. One part of this consists of resetting the object owner.</p>

<p>I'm calling icacls.exe like this.</p>

<pre><code>$ICACLSResult = icacls "$OBJPath\*" /setowner $OBJOwner /t /c /q | Out-String
</code></pre>

<p>I then check the string <code>$ICACLSResult</code> for a specific text to check whether errors occured or not and also append the string itself to my error message.</p>

<pre><code>if($ICACLSResult -notmatch "bei 0 Dateien ist ein Verarbeitungsfehler aufgetreten" -and $ICACLSResult -notmatch "Failed processing 0 files")
{
    Add-Error -SED ($ErrorMSG + $ICACLSResult) -Level 2 -AddErrorInfo $False -ExitScript $False

    Throw("AnErrorOccured")
}
</code></pre>

<p>However, the actual content of the variable <code>$ICACLSResult</code> looks like this.</p>

<pre><code>Successfully processed 13970 files; Failed processing 5 files
</code></pre>

<p>But when I run the same command directly within the console, it looks like this.</p>

<pre><code>PS C:\Users\User&gt; $ICACLSResult = icacls "$OBJPath\*" /setowner $OBJOwner /t /c /q | Out-String

D:\Folder1\Folder2\Folder3\File.txt: The system cannot find the path specified.
D:\Folder1\Folder2\Folder3\File.txt: The system cannot find the path specified.
D:\Folder1\Folder2\Folder3\File.txt: The system cannot find the path specified.
D:\Folder1\Folder2\Folder3\File.txt: The system cannot find the path specified.
D:\Folder1\Folder2\Folder3\File.txt: The system cannot find the path specified.

PS C:\Users\User&gt;
</code></pre>

<p>But again <code>$ICACLSResult</code> looks like this.</p>

<pre><code>PS C:\Users\User&gt; $ICACLSResult
Successfully processed 13970 files; Failed processing 5 files

PS C:\Users\User&gt;
</code></pre>

<p>Without these detailed error messages, there's no use logging the variable <code>$ICACLSResult</code> since all it says is how many files failed but not which files specifically.</p>

<p>How can I get this additional info as well and store it in <code>$ICACLSResult</code>? All I want are the paths of the failed files.</p>

<p><strong>What I've also tried so far</strong></p>

<ol>
<li><code>$ICACLSResult = icacls "$OBJPath\*" /setowner $OBJOwner /t /c /q 2&gt;&amp;1</code></li>
<li><code>$ICACLSResult = icacls "$OBJPath\*" /setowner $OBJOwner /t /c /q 2&gt;&amp;1 | Out-String -Stream -Width 9999</code></li>
<li><code>$ICACLSResult = icacls "$OBJPath\*" /setowner $OBJOwner /t /c /q</code></li>
</ol>

<p><strong>Why they don't work</strong></p>

<ol>
<li>Too much information, contains linebreaks</li>
<li>Too much information, contains linebreaks</li>
<li>Desired information still not in variable <code>$ICACLSResult</code></li>
</ol>