title: VBA - WScript.Shell - Executing A PowerShell Command
body: <p><strong>I want to execute a PowerShell Command from VBA using WScript.Shell</strong></p>
  <p>Here is the string that gets sent to WScript.Shell:</p>
  <pre><code>Powershell -ExecutionPolicy Bypass -Command $FilesInPathway = get-childitem -path &quot;C:\Users\Me\Desktop\NewFolder&quot; -recurse -attributes !directory; foreach ($file in $FilesInPathway){$tempName = $file.name; $tempName = $tempName.replace(&quot;.TXT&quot;,&quot;.txt&quot;); Rename-Item $file.fullname $tempName}
  </code></pre>
  <p>And here is the PowerShell Command I want to execute. Note this command does successfully execute in PowerShell.</p>
  <pre><code>$FilesInPathway = get-childitem -path &quot;C:\Users\Me\Desktop\NewFolder&quot; -recurse -attributes !directory; foreach ($file in $FilesInPathway){$tempName = $file.name; $tempName = $tempName.replace(&quot;.TXT&quot;,&quot;.txt&quot;); Rename-Item $file.fullname $tempName}
  </code></pre>
  <p>Do not get information that there is an issue executing PowerShell Command, but the Command does not execute and files are not renamed. Msgbox returns no information. Blank form. Want to avoid switching to a PowerShell script or writing this using DIR and NAME.</p>
  <pre><code>strCommand = &quot;Powershell -ExecutionPolicy Bypass -Command $FilesInPathway = get-childitem -path &quot; &amp; &quot;&quot;&quot;&quot; &amp; dataPacket.filePath &amp; &quot;&quot;&quot;&quot; &amp; &quot; -recurse -Attributes !Directory; foreach ($file in $FilesInPathway){$tempName = $file.name; $tempName = $tempName.replace(&quot; &amp; &quot;&quot;&quot;&quot; &amp; dataPacket.stringOld &amp; &quot;&quot;&quot;&quot; &amp; &quot;,&quot; &amp; &quot;&quot;&quot;&quot; &amp; dataPacket.stringNew &amp; &quot;&quot;&quot;&quot; &amp; &quot;); Rename-Item $file.fullname $tempName}&quot;
  Set WshShell = CreateObject(&quot;WScript.Shell&quot;)
  Set WshShellExec = WshShell.Exec(strCommand)
  strOutput = WshShellExec.StdOut.ReadAll
  MsgBox strOutput
  </code></pre>
  <p><strong>UPDATE I</strong></p>
  <p>New Command does traverse the directory tree and returns correct number of files in that tree. It must be the foreach function I am passing that is not executing. But when I run the debug.print version of string directly in Powershell, it executes OK - all files are renamed.</p>
  <pre><code>strCommand = &quot;Powershell $FilesInPathway = get-childitem -path &quot; &amp; &quot;&quot;&quot;&quot; &amp; dataPacket.filePath &amp; &quot;&quot;&quot;&quot; &amp; &quot; -recurse -attributes !directory; $FilesInPathway.count&quot;
  Set WshShell = CreateObject(&quot;WScript.Shell&quot;)
  Set WshShellExec = WshShell.Exec(strCommand)
  Do While WshShellExec.Status = 0
       Application.Wait (Now() + TimeValue(&quot;0:00:01&quot;))
  Loop
  strOutput = WshShellExec.StdOut.ReadAll
  MsgBox strOutput
  </code></pre>
  <p>The string that is passed to WshShell:</p>
  <pre><code>Powershell $FilesInPathway = get-childitem -path &quot;C:\Users\Me\Desktop\NewFolder&quot; -recurse -attributes !directory; $FilesInPathway.count
  </code></pre>
  <p>The string that is passed to PowerShell:</p>
  <pre><code>$FilesInPathway = get-childitem -path &quot;C:\Users\Me\Desktop\NewFolder&quot; -recurse -attributes !directory; $FilesInPathway.count
  </code></pre>
  <p>Note I have tried to use the StdIn.Write method to isolate Commands but am getting error.</p>
  <pre><code>WshShellExec.StdIn.Write &quot;$FilesInPathway.count&quot;
  </code></pre>
  <p><a href="https://i.stack.imgur.com/FhSuF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FhSuF.png" alt="enter image description here" /></a></p>
  <p><strong>UPDATE II</strong></p>
  <p>Running Shell() with a type or enum declared may generate malicious macro warning and kill excel application? Have not isolated code that causes this.</p>
  <p><a href="https://i.stack.imgur.com/YuPfp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YuPfp.png" alt="enter image description here" /></a></p>
  <p>The following will enter infinite loop if -noexit included with Command
  Do While WshShellExec.Status = 0
  Application.Wait (Now() + TimeValue(&quot;0:00:01&quot;))
  Loop</p>
  <p><strong>ANSWER</strong></p>
  <p>Of course this would be a syntax problem. Need to enclose parameters of Replace method with ' '. If filepath contains a whitespace, it too needs enclosure with ' '. If entered directly into PowerShell, enclosure with &quot; &quot; works fine. So the Command is:</p>
  <pre><code>strCommand = &quot;Powershell $FilesInPathway = get-childitem -path &quot; &amp; &quot;'&quot; &amp; dataPacket.filePath &amp; &quot;'&quot; &amp; &quot; -recurse -attributes !directory; foreach ($file in $FilesInPathway){$tempName = $file.name; $tempName = $tempName.replace('&quot; &amp; dataPacket.stringOld &amp; &quot;','&quot; &amp; dataPacket.stringNew &amp; &quot;'); Rename-Item $file.fullname $tempName}&quot;
  </code></pre>
  <p>This seems to explain why: <a href="https://stackoverflow.com/a/45762288/9721351">https://stackoverflow.com/a/45762288/9721351</a></p>