<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: arnostorg</title>
    <description>The latest articles on DEV Community by arnostorg (@arnostorg).</description>
    <link>https://dev.to/arnostorg</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1724213%2Fbf22ff52-4243-4658-b3c1-0722d1a13b17.webp</url>
      <title>DEV Community: arnostorg</title>
      <link>https://dev.to/arnostorg</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/arnostorg"/>
    <language>en</language>
    <item>
      <title>PowerShell Set-Content vs Add-Content: The Real Difference (and Why Your Log Only Has the Last Line)</title>
      <dc:creator>arnostorg</dc:creator>
      <pubDate>Fri, 27 Nov 2026 08:21:29 +0000</pubDate>
      <link>https://dev.to/arnostorg/powershell-set-content-vs-add-content-the-real-difference-and-why-your-log-only-has-the-last-line-2ilb</link>
      <guid>https://dev.to/arnostorg/powershell-set-content-vs-add-content-the-real-difference-and-why-your-log-only-has-the-last-line-2ilb</guid>
      <description>&lt;p&gt;Your script checks three servers and writes a line for each one to a log. You open the log afterwards and there is exactly one line in it: the last server. No error, no warning. The loop ran three times, and the log only remembers one of them.&lt;/p&gt;

&lt;p&gt;The cause is one word: you used &lt;code&gt;Set-Content&lt;/code&gt; where you needed &lt;code&gt;Add-Content&lt;/code&gt;. This tip shows the real difference between the two, the stale-file trap that catches even people who know the difference, and a log pattern that gives you a clean file on every run.&lt;/p&gt;

&lt;h2&gt;
  
  
  The one-line difference
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Set-Content&lt;/code&gt; &lt;strong&gt;replaces&lt;/strong&gt; everything in the file with what you give it. If the file does not exist, it creates it.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Add-Content&lt;/code&gt; &lt;strong&gt;appends&lt;/strong&gt; to the end of the file. If the file does not exist, it creates it too.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's it. Both write text, both default to UTF-8 without a BOM in PowerShell 7, and both take input from &lt;code&gt;-Value&lt;/code&gt; or the pipeline. The difference only shows up when you write more than once.&lt;/p&gt;

&lt;h2&gt;
  
  
  The bug: Set-Content in a loop
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$s&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="s1"&gt;'web01'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'web02'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'db01'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"checked $s"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Content&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;log&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Content&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;log&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;checked db01
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every pass through the loop overwrote the file. The first two lines were written, then wiped. Swap in &lt;code&gt;Add-Content&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$s&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="s1"&gt;'web01'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'web02'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'db01'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"checked $s"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Add&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Content&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;run2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;log&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Content&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;run2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;log&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;checked web01
checked web02
checked db01
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All three lines are there.&lt;/p&gt;

&lt;h2&gt;
  
  
  The opposite bug: Add-Content never forgets
&lt;/h2&gt;

&lt;p&gt;Run that &lt;code&gt;Add-Content&lt;/code&gt; loop a second time and count the lines:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$s&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="s1"&gt;'web01'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'web02'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'db01'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"checked $s"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Add&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Content&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;run2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;log&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Content&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;run2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Count&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Add-Content&lt;/code&gt; keeps everything from earlier runs. For a long-running log that is what you want. For a "results of this run" file, it means yesterday's results look like today's.&lt;/p&gt;

&lt;h2&gt;
  
  
  The log pattern: Set-Content once, then Add-Content
&lt;/h2&gt;

&lt;p&gt;Start the run with &lt;code&gt;Set-Content&lt;/code&gt; to reset the file, then append everything after it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$log&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'.\deploy.log'&lt;/span&gt;
&lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Content&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="nv"&gt;$log&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Value&lt;/span&gt; &lt;span class="s2"&gt;"Deploy started $(Get-Date -Format 'yyyy-MM-dd HH:mm')"&lt;/span&gt;

&lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$s&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="s1"&gt;'web01'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'web02'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Add&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Content&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="nv"&gt;$log&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Value&lt;/span&gt; &lt;span class="s2"&gt;"deployed $s"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;Add&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Content&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="nv"&gt;$log&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Value&lt;/span&gt; &lt;span class="s1"&gt;'Deploy finished'&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Content&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="nv"&gt;$log&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Count&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Four lines every time: the header, two servers, the footer. Run it again and it is still four.&lt;/p&gt;

&lt;h2&gt;
  
  
  The stale-file trap: Set-Content with nothing to write
&lt;/h2&gt;

&lt;p&gt;This one surprised me while testing. Say yesterday's run found a failure and wrote it to &lt;code&gt;failed.txt&lt;/code&gt;. Today everything passes, so your filter returns nothing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="s1"&gt;'web01 FAILED'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Content&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;failed&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;txt&lt;/span&gt;

&lt;span class="nv"&gt;$failed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'web01 OK'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'web02 OK'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Where&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;$_&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;like&lt;/span&gt; &lt;span class="s1"&gt;'*FAILED*'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nv"&gt;$failed&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Content&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;failed&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;txt&lt;/span&gt;
&lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Content&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;failed&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;txt&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;web01 FAILED
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The file still says web01 failed. When &lt;strong&gt;nothing comes down the pipeline&lt;/strong&gt;, &lt;code&gt;Set-Content&lt;/code&gt; never runs its write, so the old content stays. (In my test it did not even create the file when it was missing.) Anyone reading &lt;code&gt;failed.txt&lt;/code&gt; sees yesterday's failure as today's.&lt;/p&gt;

&lt;p&gt;Pass the variable with &lt;code&gt;-Value&lt;/code&gt; instead of piping it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Content&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;failed&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;txt&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Value&lt;/span&gt; &lt;span class="nv"&gt;$failed&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Item&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;failed&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;txt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Length&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With &lt;code&gt;-Value&lt;/code&gt; the cmdlet runs even when &lt;code&gt;$failed&lt;/code&gt; is empty, so the file is emptied. Use &lt;code&gt;-Value&lt;/code&gt; for any "current state" file that is allowed to be empty.&lt;/p&gt;

&lt;h2&gt;
  
  
  Two Add-Content gotchas
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;It glues onto a last line that has no newline.&lt;/strong&gt; &lt;code&gt;Add-Content&lt;/code&gt; writes at the very end of the file. If the file was written with &lt;code&gt;-NoNewline&lt;/code&gt; (or by a tool that skips the final line break), your new text lands on the same line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Content&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;txt&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Value&lt;/span&gt; &lt;span class="s1"&gt;'first'&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;NoNewline&lt;/span&gt;
&lt;span class="nc"&gt;Add&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Content&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;txt&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Value&lt;/span&gt; &lt;span class="s1"&gt;'second'&lt;/span&gt;
&lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Content&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;txt&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;firstsecond
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;It creates the file, not the folder.&lt;/strong&gt; &lt;code&gt;Add-Content -Path .\logs\today.log&lt;/code&gt; fails with "Could not find a part of the path" when &lt;code&gt;logs&lt;/code&gt; does not exist. Create the folder once at the top of the script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="nc"&gt;New&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Item&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;ItemType&lt;/span&gt; &lt;span class="nc"&gt;Directory&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;logs&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Force&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Out&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="kc"&gt;Null&lt;/span&gt;
&lt;span class="nc"&gt;Add&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Content&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;logs&lt;/span&gt;\&lt;span class="n"&gt;today&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;log&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Value&lt;/span&gt; &lt;span class="s1"&gt;'run started'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  When you have many lines: write once
&lt;/h2&gt;

&lt;p&gt;Each &lt;code&gt;Add-Content&lt;/code&gt; call opens the file, writes and closes it again. For a handful of lines that doesn't matter. For thousands it does. Collect the lines, then write them in one go:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$lines&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$s&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="s1"&gt;'web01'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'web02'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'db01'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s2"&gt;"checked $s"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nv"&gt;$lines&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Content&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;run3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;log&lt;/span&gt;
&lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Content&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;run3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;log&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;checked web01
checked web02
checked db01
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In my test, 2,000 separate &lt;code&gt;Add-Content&lt;/code&gt; calls took about 315 ms, while piping the same 2,000 lines into one &lt;code&gt;Set-Content&lt;/code&gt; took about 23 ms. Both files ended up with 2,000 lines. Your numbers will differ, but the ratio is the point.&lt;/p&gt;

&lt;h2&gt;
  
  
  Preview before you overwrite: -WhatIf
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Set-Content&lt;/code&gt; is destructive, so it supports &lt;code&gt;-WhatIf&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Content&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;run3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;log&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Value&lt;/span&gt; &lt;span class="s1"&gt;'x'&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;WhatIf&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;What if: Performing the operation "Set Content" on target "Path: C:\Work\run3.log".
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nothing was written.&lt;/p&gt;

&lt;h2&gt;
  
  
  Quick reference
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;You want&lt;/th&gt;
&lt;th&gt;Use&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Replace the whole file&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Set-Content -Path $file -Value $text&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Add a line to the end&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Add-Content -Path $file -Value $line&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;A fresh log on every run&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;Set-Content&lt;/code&gt; for the first line, &lt;code&gt;Add-Content&lt;/code&gt; after it&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;A file that may need to become empty&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;Set-Content -Path $file -Value $result&lt;/code&gt; (not `$result \&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Thousands of lines&lt;/td&gt;
&lt;td&gt;Collect them, then pipe once into {% raw %}&lt;code&gt;Set-Content&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;To check first&lt;/td&gt;
&lt;td&gt;Add &lt;code&gt;-WhatIf&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;I tested every block above in PowerShell 7.4.6. The demo ran on Linux, so the &lt;code&gt;-WhatIf&lt;/code&gt; path above is shown as it would look on Windows; the behavior is the same. Docs: &lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/set-content" rel="noopener noreferrer"&gt;Set-Content on Microsoft Learn&lt;/a&gt;, &lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/add-content" rel="noopener noreferrer"&gt;Add-Content on Microsoft Learn&lt;/a&gt;, and &lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_character_encoding" rel="noopener noreferrer"&gt;about_Character_Encoding&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Related tips: &lt;a href="https://dev.to/arnostorg/powershell-set-content-write-text-to-files-fast-1dh4"&gt;Set-Content basics for writing text to files&lt;/a&gt;, &lt;a href="https://dev.to/arnostorg/powershell-add-content-append-text-to-files-fast-dag"&gt;Add-Content basics for appending text&lt;/a&gt;, &lt;a href="https://dev.to/arnostorg/powershell-out-file-vs-set-content-encoding-is-the-real-difference-m9l"&gt;Out-File vs Set-Content and why encoding matters&lt;/a&gt;, and &lt;a href="https://dev.to/arnostorg/powershell-get-content-tail-the-last-lines-of-any-log-34b5"&gt;tailing the last lines of a log with Get-Content&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Practice it hands-on
&lt;/h2&gt;

&lt;p&gt;Overwrite-versus-append mistakes stick once you have made them yourself. &lt;a href="https://windows-cli.arnost.org/en" rel="noopener noreferrer"&gt;CMD Master's free interactive PowerShell, CMD and Bash terminal lessons&lt;/a&gt; run in your browser, so you can practice writing and appending to files without touching a real log.&lt;/p&gt;

&lt;h2&gt;
  
  
  Your turn: what does this output?
&lt;/h2&gt;

&lt;p&gt;What does the last line print?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Content&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;txt&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Value&lt;/span&gt; &lt;span class="s1"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'b'&lt;/span&gt;
&lt;span class="nc"&gt;Add&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Content&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;txt&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Value&lt;/span&gt; &lt;span class="s1"&gt;'c'&lt;/span&gt;
&lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Content&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;txt&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Value&lt;/span&gt; &lt;span class="s1"&gt;'d'&lt;/span&gt;
&lt;span class="nc"&gt;Add&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Content&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;txt&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Value&lt;/span&gt; &lt;span class="s1"&gt;'e'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'f'&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Content&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;txt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;join&lt;/span&gt; &lt;span class="s1"&gt;','&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Drop your answer in the comments before you run it. I'll reply with the answer and why.&lt;/p&gt;

</description>
      <category>powershell</category>
      <category>scripting</category>
      <category>windows</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Windows MOVE Command: Move Files Into a Folder Without Renaming Them by Accident</title>
      <dc:creator>arnostorg</dc:creator>
      <pubDate>Wed, 25 Nov 2026 08:21:49 +0000</pubDate>
      <link>https://dev.to/arnostorg/windows-move-command-move-files-into-a-folder-without-renaming-them-by-accident-31n4</link>
      <guid>https://dev.to/arnostorg/windows-move-command-move-files-into-a-folder-without-renaming-them-by-accident-31n4</guid>
      <description>&lt;p&gt;You type &lt;code&gt;move report.txt D:\Archive&lt;/code&gt;, CMD says &lt;code&gt;1 file(s) moved.&lt;/code&gt;, and then &lt;code&gt;D:\Archive&lt;/code&gt; turns out to be a &lt;strong&gt;file&lt;/strong&gt; named "Archive", not a folder with your report in it. &lt;code&gt;MOVE&lt;/code&gt; is simple, but it quietly does three different jobs: move into a folder, rename, and overwrite. Here is how to get the one you meant.&lt;/p&gt;

&lt;h2&gt;
  
  
  The command
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight batchfile"&gt;&lt;code&gt;&lt;span class="nb"&gt;move&lt;/span&gt; &lt;span class="no"&gt;C&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;\&lt;span class="nc"&gt;Reports&lt;/span&gt;\&lt;span class="n"&gt;report&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;txt&lt;/span&gt; &lt;span class="no"&gt;D&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;\&lt;span class="nc"&gt;Archive&lt;/span&gt;\
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The trailing backslash on &lt;code&gt;D:\Archive\&lt;/code&gt; says "this is a folder". If the folder exists, the file lands inside it and CMD prints:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        1 file(s) moved.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the folder doesn't exist, the move fails instead of guessing. That failure is what you want.&lt;/p&gt;

&lt;h2&gt;
  
  
  Trap 1: a missing folder becomes a new file name
&lt;/h2&gt;

&lt;p&gt;Without the trailing backslash, MOVE reads the target as "move &lt;strong&gt;and rename&lt;/strong&gt; to this name" whenever no folder with that name exists:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight batchfile"&gt;&lt;code&gt;&lt;span class="k"&gt;rem&lt;/span&gt; &lt;span class="no"&gt;D&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;\&lt;span class="nc"&gt;Archive&lt;/span&gt; &lt;span class="n"&gt;does&lt;/span&gt; &lt;span class="k"&gt;not&lt;/span&gt; &lt;span class="k"&gt;exist&lt;/span&gt; &lt;span class="n"&gt;yet&lt;/span&gt;
&lt;span class="nb"&gt;move&lt;/span&gt; &lt;span class="no"&gt;C&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;\&lt;span class="nc"&gt;Reports&lt;/span&gt;\&lt;span class="n"&gt;report&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;txt&lt;/span&gt; &lt;span class="no"&gt;D&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;\&lt;span class="nc"&gt;Archive&lt;/span&gt;

&lt;span class="k"&gt;rem&lt;/span&gt; &lt;span class="nc"&gt;Result&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="n"&gt;called&lt;/span&gt; &lt;span class="no"&gt;D&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;\&lt;span class="nc"&gt;Archive&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;no&lt;/span&gt; &lt;span class="n"&gt;extension&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;report&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;txt&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;gone&lt;/span&gt; &lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="no"&gt;C&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;\&lt;span class="nc"&gt;Reports&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The fix is to make sure the folder exists first, then move into it with a trailing backslash:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight batchfile"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;not&lt;/span&gt; &lt;span class="k"&gt;exist&lt;/span&gt; &lt;span class="s2"&gt;"D:\Archive\" mkdir "&lt;/span&gt;&lt;span class="no"&gt;D&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;\&lt;span class="nc"&gt;Archive&lt;/span&gt;&lt;span class="s2"&gt;"
move "&lt;/span&gt;&lt;span class="no"&gt;C&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;\&lt;span class="nc"&gt;Reports&lt;/span&gt;\&lt;span class="n"&gt;report&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;txt&lt;/span&gt;&lt;span class="s2"&gt;" "&lt;/span&gt;&lt;span class="no"&gt;D&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;\&lt;span class="nc"&gt;Archive&lt;/span&gt;\&lt;span class="s2"&gt;"
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With wildcards, MOVE refuses to merge several files into one name. If the target folder is missing, you get &lt;code&gt;Cannot move multiple files to a single file.&lt;/code&gt; instead of a silent rename:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight batchfile"&gt;&lt;code&gt;&lt;span class="k"&gt;rem&lt;/span&gt; &lt;span class="no"&gt;D&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;\&lt;span class="nc"&gt;Q2Reports&lt;/span&gt; &lt;span class="n"&gt;does&lt;/span&gt; &lt;span class="k"&gt;not&lt;/span&gt; &lt;span class="k"&gt;exist&lt;/span&gt; &lt;span class="n"&gt;yet&lt;/span&gt;
&lt;span class="nb"&gt;move&lt;/span&gt; &lt;span class="no"&gt;C&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;\&lt;span class="nc"&gt;Data&lt;/span&gt;\&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;xls&lt;/span&gt; &lt;span class="no"&gt;D&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;\&lt;span class="nc"&gt;Q2Reports&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Trap 2: overwrite prompts disappear inside batch files
&lt;/h2&gt;

&lt;p&gt;Type MOVE at the prompt and it asks before replacing a file that already exists in the target:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Overwrite D:\Archive\report.txt? (Yes/No/All):
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run the &lt;strong&gt;same line inside a .bat or .cmd file&lt;/strong&gt; and, by default, there is no prompt: the existing file is replaced. That is documented behavior, and it is how a nightly script wipes out yesterday's archive copy.&lt;/p&gt;

&lt;p&gt;Use the switches to say what you want:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Switch&lt;/th&gt;
&lt;th&gt;What it does&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;/y&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Never ask, always overwrite&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;/-y&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Always ask before overwriting, even in a batch file&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight batchfile"&gt;&lt;code&gt;&lt;span class="k"&gt;rem&lt;/span&gt; &lt;span class="nc"&gt;Interactive&lt;/span&gt; &lt;span class="n"&gt;cleanup&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ask&lt;/span&gt; &lt;span class="n"&gt;me&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;even&lt;/span&gt; &lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;script&lt;/span&gt;
&lt;span class="nb"&gt;move&lt;/span&gt; &lt;span class="o"&gt;/-&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="no"&gt;C&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;\&lt;span class="nc"&gt;Reports&lt;/span&gt;\&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;txt&lt;/span&gt; &lt;span class="no"&gt;D&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;\&lt;span class="nc"&gt;Archive&lt;/span&gt;\

&lt;span class="k"&gt;rem&lt;/span&gt; &lt;span class="nc"&gt;Unattended&lt;/span&gt; &lt;span class="n"&gt;job&lt;/span&gt; &lt;span class="n"&gt;where&lt;/span&gt; &lt;span class="n"&gt;replacing&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;intended&lt;/span&gt;
&lt;span class="nb"&gt;move&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="no"&gt;C&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;\&lt;span class="nc"&gt;Reports&lt;/span&gt;\&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;txt&lt;/span&gt; &lt;span class="no"&gt;D&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;\&lt;span class="nc"&gt;Archive&lt;/span&gt;\
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;COPYCMD&lt;/code&gt; environment variable can preset &lt;code&gt;/y&lt;/code&gt;, so on a machine where someone set &lt;code&gt;COPYCMD=/Y&lt;/code&gt;, plain &lt;code&gt;move&lt;/code&gt; overwrites at the prompt too. Check it with &lt;code&gt;echo %COPYCMD%&lt;/code&gt;; &lt;code&gt;/-y&lt;/code&gt; on the command line wins over it.&lt;/p&gt;

&lt;p&gt;For an unattended script that must &lt;strong&gt;never&lt;/strong&gt; replace an older file, check before moving:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight batchfile"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;exist&lt;/span&gt; &lt;span class="s2"&gt;"D:\Archive\report.txt"&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="n"&gt;report&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;txt&lt;/span&gt; &lt;span class="n"&gt;already&lt;/span&gt; &lt;span class="n"&gt;archived&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;skipping&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nb"&gt;move&lt;/span&gt; &lt;span class="s2"&gt;"C:\Reports\report.txt"&lt;/span&gt; &lt;span class="s2"&gt;"D:\Archive\"
)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Trap 3: renaming a folder vs moving it inside another
&lt;/h2&gt;

&lt;p&gt;MOVE also renames folders. Point it at the current folder path and the new name:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight batchfile"&gt;&lt;code&gt;&lt;span class="nb"&gt;move&lt;/span&gt; &lt;span class="no"&gt;C&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;\&lt;span class="nc"&gt;Projects&lt;/span&gt;\&lt;span class="nc"&gt;WebApp&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;old&lt;/span&gt; &lt;span class="no"&gt;C&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;\&lt;span class="nc"&gt;Projects&lt;/span&gt;\&lt;span class="nc"&gt;WebApp&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;2025&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But if &lt;code&gt;C:\Projects\WebApp-2025&lt;/code&gt; &lt;strong&gt;already exists&lt;/strong&gt;, MOVE doesn't rename anything. It moves &lt;code&gt;WebApp-old&lt;/code&gt; inside it, and you end up with &lt;code&gt;C:\Projects\WebApp-2025\WebApp-old&lt;/code&gt;. Run &lt;code&gt;dir C:\Projects&lt;/code&gt; first if you're not sure the new name is free.&lt;/p&gt;

&lt;p&gt;For renaming a single file without moving it, &lt;code&gt;ren&lt;/code&gt; is clearer to read in scripts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight batchfile"&gt;&lt;code&gt;&lt;span class="nb"&gt;ren&lt;/span&gt; &lt;span class="no"&gt;C&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;\&lt;span class="nc"&gt;Reports&lt;/span&gt;\&lt;span class="n"&gt;report&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;txt&lt;/span&gt; &lt;span class="n"&gt;report&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;2026&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;txt&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Names with spaces need quotes
&lt;/h2&gt;

&lt;p&gt;Without quotes, CMD splits the path at the space and MOVE sees too many arguments (&lt;code&gt;The syntax of the command is incorrect.&lt;/code&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight batchfile"&gt;&lt;code&gt;&lt;span class="nb"&gt;move&lt;/span&gt; &lt;span class="s2"&gt;"C:\Users\Arnost\Downloads\Q3 report.pdf"&lt;/span&gt; &lt;span class="s2"&gt;"D:\Finance Docs\"
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Quote both paths every time in scripts, even when today's names have no spaces.&lt;/p&gt;

&lt;h2&gt;
  
  
  Check the result in a batch file
&lt;/h2&gt;

&lt;p&gt;MOVE sets the exit code, so a script can stop when a move fails (missing source, missing folder, file in use):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight batchfile"&gt;&lt;code&gt;&lt;span class="nd"&gt;@echo&lt;/span&gt; &lt;span class="n"&gt;off&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;not&lt;/span&gt; &lt;span class="k"&gt;exist&lt;/span&gt; &lt;span class="s2"&gt;"D:\Archive\" mkdir "&lt;/span&gt;&lt;span class="no"&gt;D&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;\&lt;span class="nc"&gt;Archive&lt;/span&gt;&lt;span class="s2"&gt;"
move /-y "&lt;/span&gt;&lt;span class="no"&gt;C&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;\&lt;span class="nc"&gt;Reports&lt;/span&gt;\&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;txt&lt;/span&gt;&lt;span class="s2"&gt;" "&lt;/span&gt;&lt;span class="no"&gt;D&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;\&lt;span class="nc"&gt;Archive&lt;/span&gt;\&lt;span class="s2"&gt;"
if errorlevel 1 (
    echo Move FAILED with code %errorlevel%
    exit /b 1
)
echo Reports archived.
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  MOVE, COPY, or XCOPY?
&lt;/h2&gt;

&lt;p&gt;Use MOVE when the source should be gone afterwards. Use COPY when you need to keep the original, and XCOPY when you're copying a whole folder tree with subfolders, which I covered in &lt;a href="https://dev.to/arnostorg/windows-xcopy-command-copy-a-folder-and-all-its-subfolders-2ib"&gt;Windows XCOPY Command: Copy a Folder and All Its Subfolders&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Related CMD tips:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/arnostorg/windows-copy-command-duplicate-files-fast-from-cmd-49i9"&gt;Windows COPY Command: Duplicate Files Fast From CMD&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/arnostorg/windows-mkdir-command-create-folders-fast-from-cmd-47c3"&gt;Windows MKDIR Command: Create Folders Fast From CMD&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/arnostorg/windows-del-command-delete-files-safely-from-cmd-2nea"&gt;Windows DEL Command: Delete Files Safely From CMD&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Prefer PowerShell? &lt;a href="https://dev.to/arnostorg/move-and-rename-files-in-powershell-move-item-explained-35kl"&gt;Move and Rename Files in PowerShell: Move-Item Explained&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Official reference: &lt;a href="https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/move" rel="noopener noreferrer"&gt;move on Microsoft Learn&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Practice it without risking real files
&lt;/h2&gt;

&lt;p&gt;Want to see the "missing folder becomes a file" trap happen without losing a real report? &lt;a href="https://windows-cli.arnost.org/en" rel="noopener noreferrer"&gt;CMD Master's free interactive CMD lessons&lt;/a&gt; run in a browser terminal, so you can move, rename, and break files safely. It also covers PowerShell and Bash.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick check:&lt;/strong&gt; &lt;code&gt;C:\Temp&lt;/code&gt; contains &lt;code&gt;a.txt&lt;/code&gt; and &lt;code&gt;b.txt&lt;/code&gt;, and &lt;code&gt;C:\Done&lt;/code&gt; does not exist. What does &lt;code&gt;move C:\Temp\a.txt C:\Done&lt;/code&gt; leave on disk? Answer in the comments.&lt;/p&gt;

</description>
      <category>windows</category>
      <category>commandline</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Export Data to CSV in PowerShell: Export-Csv Explained</title>
      <dc:creator>arnostorg</dc:creator>
      <pubDate>Mon, 23 Nov 2026 08:26:03 +0000</pubDate>
      <link>https://dev.to/arnostorg/export-data-to-csv-in-powershell-export-csv-explained-2d58</link>
      <guid>https://dev.to/arnostorg/export-data-to-csv-in-powershell-export-csv-explained-2d58</guid>
      <description>&lt;p&gt;Someone asks for "a list of the files in that folder, in Excel, by lunch". In PowerShell that is one pipeline: pick the columns with &lt;code&gt;Select-Object&lt;/code&gt;, then hand the objects to &lt;strong&gt;Export-Csv&lt;/strong&gt;. Each object becomes one row, and each property becomes one column.&lt;/p&gt;

&lt;p&gt;This tip covers the jobs you will actually use it for: exporting a file list, logging repeated runs, making Excel on a European machine open the file properly, keeping accented names intact, reading the file back, and not overwriting a report you needed. Every snippet below was run in PowerShell 7.4, and the output shown is the real output. Where Windows PowerShell 5.1 behaves differently, I say so.&lt;/p&gt;

&lt;h2&gt;
  
  
  Set up a safe demo folder
&lt;/h2&gt;

&lt;p&gt;Run this first. It creates a throwaway folder in your temp directory with three small files and a fixed timestamp, so your output matches mine:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$demo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Join&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;IO&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nc"&gt;GetTempPath&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="s1"&gt;'export-csv-demo'&lt;/span&gt;
&lt;span class="nc"&gt;New&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Item&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;ItemType&lt;/span&gt; &lt;span class="nc"&gt;Directory&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Join&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="nv"&gt;$demo&lt;/span&gt; &lt;span class="s1"&gt;'Logs'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Force&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Out&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="kc"&gt;Null&lt;/span&gt;
&lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Location&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="nv"&gt;$demo&lt;/span&gt;
&lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Content&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="nc"&gt;Logs&lt;/span&gt;\&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;log&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Value&lt;/span&gt; &lt;span class="s1"&gt;'service started, 3 jobs ok'&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;NoNewline&lt;/span&gt;
&lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Content&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="nc"&gt;Logs&lt;/span&gt;\&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;log&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Value&lt;/span&gt; &lt;span class="s1"&gt;'disk full on D:'&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;NoNewline&lt;/span&gt;
&lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Content&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="nc"&gt;Logs&lt;/span&gt;\&lt;span class="n"&gt;report&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;txt&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Value&lt;/span&gt; &lt;span class="s1"&gt;'Quarterly numbers'&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;NoNewline&lt;/span&gt;
&lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;ChildItem&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="nc"&gt;Logs&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;File&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="k"&gt;ForEach&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;$_&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;LastWriteTime&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'2026-11-20 08:15'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;ChildItem&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="nc"&gt;Logs&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;File&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Select&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Property&lt;/span&gt; &lt;span class="nc"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Length&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Name       Length
----       ------
app.log        26
error.log      15
report.txt     17
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All the later snippets assume you are still inside that folder.&lt;/p&gt;

&lt;h2&gt;
  
  
  Job 1: Export a file list to CSV
&lt;/h2&gt;

&lt;p&gt;Select the properties you want &lt;strong&gt;first&lt;/strong&gt;, then export:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;ChildItem&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="nc"&gt;Logs&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;File&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="nc"&gt;Select&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Property&lt;/span&gt; &lt;span class="nc"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Length&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        @&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;Name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'LastWriteTime'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nc"&gt;Expression&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;$_&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;LastWriteTime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ToString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'yyyy-MM-dd HH:mm'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="nc"&gt;Export&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Csv&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;files&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;csv&lt;/span&gt;

&lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Content&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;files&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;csv&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"Name","Length","LastWriteTime"
"app.log","26","2026-11-20 08:15"
"error.log","15","2026-11-20 08:15"
"report.txt","17","2026-11-20 08:15"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three things to notice:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Select-Object&lt;/code&gt; decides the columns. Without it you get every property of &lt;code&gt;FileInfo&lt;/code&gt; (dozens of columns, such as &lt;code&gt;PSPath&lt;/code&gt;, &lt;code&gt;Attributes&lt;/code&gt; and &lt;code&gt;Directory&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;The calculated property turns &lt;code&gt;LastWriteTime&lt;/code&gt; into &lt;code&gt;yyyy-MM-dd HH:mm&lt;/code&gt;. A raw &lt;code&gt;DateTime&lt;/code&gt; is written using your culture's format, which may not sort or parse correctly on someone else's machine.&lt;/li&gt;
&lt;li&gt;Every value is quoted and written as text. CSV has no types, and that matters when you import the file again (more on that below).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  -NoTypeInformation: needed in 5.1, default in 7
&lt;/h2&gt;

&lt;p&gt;In &lt;strong&gt;Windows PowerShell 5.1&lt;/strong&gt;, Export-Csv writes a &lt;code&gt;#TYPE&lt;/code&gt; line above the header by default, and Excel shows it as a junk first row. That is why so many scripts end with &lt;code&gt;-NoTypeInformation&lt;/code&gt;. Since &lt;strong&gt;PowerShell 6.0&lt;/strong&gt;, no type line is the default, and &lt;code&gt;-NoTypeInformation&lt;/code&gt; is accepted but does nothing. If you want the old behavior in 7, you have to ask for it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="c1"&gt;# PowerShell 7: opt back in to the old 5.1 default&lt;/span&gt;
&lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;ChildItem&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="nc"&gt;Logs&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;File&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="nc"&gt;Select&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Property&lt;/span&gt; &lt;span class="nc"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Length&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="nc"&gt;Export&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Csv&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;typed&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;csv&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;IncludeTypeInformation&lt;/span&gt;

&lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Content&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;typed&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;csv&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;TotalCount&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#TYPE Selected.System.IO.FileInfo
"Name","Length"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Rule of thumb: if a script has to run on both versions, keep &lt;code&gt;-NoTypeInformation&lt;/code&gt;. It is harmless in 7 and required in 5.1.&lt;/p&gt;

&lt;h2&gt;
  
  
  Job 2: Append one row per run with -Append
&lt;/h2&gt;

&lt;p&gt;Without &lt;code&gt;-Append&lt;/code&gt;, Export-Csv replaces the file without warning. With it, you get a simple run log. The header is written once, and each run adds a row:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$run&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;pscustomobject&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;@&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Run&lt;/span&gt;    &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$run&lt;/span&gt;
        &lt;span class="nc"&gt;Files&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;ChildItem&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="nc"&gt;Logs&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;File&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Count&lt;/span&gt;
        &lt;span class="nc"&gt;Status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'OK'&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Export&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Csv&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;runs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;csv&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Append&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Content&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;runs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;csv&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"Run","Files","Status"
"1","3","OK"
"2","3","OK"
"3","3","OK"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the new object does not have a property that matches an existing column, PowerShell refuses to append instead of quietly shifting your columns:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;pscustomobject&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;@&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;Run&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nc"&gt;Status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'FAILED'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;
        &lt;span class="nc"&gt;Export&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Csv&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;runs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;csv&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Append&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;ErrorAction&lt;/span&gt; &lt;span class="nc"&gt;Stop&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$_&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Message&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Cannot append CSV content to the following file: .\runs.csv. The appended object does not have a property that corresponds to the following column: Files. To continue with mismatched properties, add the -Force parameter, and then retry the command.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Adding &lt;code&gt;-Force&lt;/code&gt; overrides this check. Only the matching properties are written, and the mismatched ones are dropped. Use it on purpose, not as a reflex.&lt;/p&gt;

&lt;h2&gt;
  
  
  Job 3: Make Excel open it in columns (-Delimiter and -UseCulture)
&lt;/h2&gt;

&lt;p&gt;In much of Europe, including Czech and German Windows, the list separator is &lt;code&gt;;&lt;/code&gt; because the comma is the decimal separator. Double-click a comma CSV there and Excel can put every row into column A. You have two fixes.&lt;/p&gt;

&lt;p&gt;Set the delimiter explicitly. The semicolon goes in single quotes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;ChildItem&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="nc"&gt;Logs&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;File&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="nc"&gt;Select&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Property&lt;/span&gt; &lt;span class="nc"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Length&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="nc"&gt;Export&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Csv&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;files&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;excel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;csv&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Delimiter&lt;/span&gt; &lt;span class="s1"&gt;';'&lt;/span&gt;

&lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Content&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;files&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;excel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;csv&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"Name";"Length"
"app.log";"26"
"error.log";"15"
"report.txt";"17"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or let Windows decide with &lt;code&gt;-UseCulture&lt;/code&gt;, which uses &lt;code&gt;(Get-Culture).TextInfo.ListSeparator&lt;/code&gt;. Here I switch the culture to Czech to show what a Czech machine produces. On a real Czech machine, skip the first line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;cultureinfo&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nc"&gt;CurrentCulture&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'cs-CZ'&lt;/span&gt;   &lt;span class="c1"&gt;# simulate a Czech Windows box&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Culture&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;TextInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ListSeparator&lt;/span&gt;

&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;pscustomobject&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;@&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;Server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'web01'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nc"&gt;FreeGB&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;12.5&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="nc"&gt;Export&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Csv&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;disk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;csv&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;UseCulture&lt;/span&gt;

&lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Content&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;disk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;csv&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;;
"Server";"FreeGB"
"web01";"12,5"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that &lt;code&gt;12.5&lt;/code&gt; was written as &lt;code&gt;12,5&lt;/code&gt;, because numbers are also converted using the current culture. &lt;code&gt;-UseCulture&lt;/code&gt; suits files that people open in Excel on the same machine. For files that another script reads, a fixed &lt;code&gt;-Delimiter&lt;/code&gt; is more predictable.&lt;/p&gt;

&lt;h2&gt;
  
  
  Job 4: Keep accents intact (-Encoding utf8BOM)
&lt;/h2&gt;

&lt;p&gt;PowerShell 7 writes UTF-8 &lt;strong&gt;without&lt;/strong&gt; a BOM by default (&lt;code&gt;utf8NoBOM&lt;/code&gt;). Excel opens a double-clicked CSV without a BOM in the legacy code page, so &lt;code&gt;Arnošt&lt;/code&gt; and &lt;code&gt;Plzeň&lt;/code&gt; come out garbled. Adding a BOM fixes that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$people&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;pscustomobject&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;@&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Arnošt'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nc"&gt;City&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Plzeň'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nv"&gt;$people&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Export&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Csv&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;nobom&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;csv&lt;/span&gt;
&lt;span class="nv"&gt;$people&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Export&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Csv&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;bom&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;csv&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Encoding&lt;/span&gt; &lt;span class="n"&gt;utf8BOM&lt;/span&gt;

&lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$file&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="s1"&gt;'users-nobom.csv'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'users-bom.csv'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$bytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Content&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="nv"&gt;$file&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;AsByteStream&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;TotalCount&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
    &lt;span class="s1"&gt;'{0,-16} {1}'&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="nv"&gt;$file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$bytes&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="k"&gt;ForEach&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s1"&gt;'{0:X2}'&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="nv"&gt;$_&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;join&lt;/span&gt; &lt;span class="s1"&gt;' '&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;users-nobom.csv  22 55 73
users-bom.csv    EF BB BF
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;EF BB BF&lt;/code&gt; is the UTF-8 BOM. The file without it starts directly with &lt;code&gt;"Us&lt;/code&gt; (&lt;code&gt;22 55 73&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Windows PowerShell 5.1 is different again. Its Export-Csv defaults to &lt;strong&gt;ASCII&lt;/strong&gt;, so &lt;code&gt;š&lt;/code&gt; and &lt;code&gt;ň&lt;/code&gt; become &lt;code&gt;?&lt;/code&gt; and the data is lost, not just displayed badly. There is no &lt;code&gt;utf8BOM&lt;/code&gt; value in 5.1, but &lt;code&gt;-Encoding UTF8&lt;/code&gt; there writes UTF-8 with a BOM, which is what Excel wants. The &lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_character_encoding" rel="noopener noreferrer"&gt;about_Character_Encoding&lt;/a&gt; page lists the defaults for both versions.&lt;/p&gt;

&lt;h2&gt;
  
  
  The classic mistake: piping Format-Table into Export-Csv
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Format-Table&lt;/code&gt; is for the screen. It outputs formatting instructions, not your data, so this is what ends up in the file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Don't do this&lt;/span&gt;
&lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;ChildItem&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="nc"&gt;Logs&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;File&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="nc"&gt;Format&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Table&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Property&lt;/span&gt; &lt;span class="nc"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Length&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="nc"&gt;Export&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Csv&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;broken&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;csv&lt;/span&gt;

&lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Content&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;broken&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;csv&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;TotalCount&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"ClassId2e4f51ef21dd47e99d3c952918aff9cd","pageHeaderEntry","pageFooterEntry","autosizeInfo","shapeInfo","groupingEntry"
"033ecb2bc07a4d43b5ef94ed5a35d280",,,,"Microsoft.PowerShell.Commands.Internal.Format.TableHeaderInfo",
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are no file names and no sizes, only formatter internals. The fix is always the same: use &lt;code&gt;Select-Object&lt;/code&gt; for the columns and keep &lt;code&gt;Format-*&lt;/code&gt; cmdlets at the very end of a pipeline that goes to the screen. I wrote up the full story in &lt;a href="https://dev.to/arnostorg/powershell-select-object-vs-format-table-the-real-difference-and-why-your-csv-is-full-of-guids-o1m"&gt;PowerShell Select-Object vs Format-Table: why your CSV is full of GUIDs&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Round trip: read it back with Import-Csv
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/import-csv" rel="noopener noreferrer"&gt;Import-Csv&lt;/a&gt; turns each row back into an object with one property per column:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$rows&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Import&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Csv&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;files&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;csv&lt;/span&gt;
&lt;span class="nv"&gt;$rows&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Format&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Table&lt;/span&gt;

&lt;span class="nv"&gt;$rows&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Length&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetType&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Name&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$rows&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Measure&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Property&lt;/span&gt; &lt;span class="nc"&gt;Length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Sum&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Sum&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Name       Length LastWriteTime
----       ------ -------------
app.log    26     2026-11-20 08:15
error.log  15     2026-11-20 08:15
report.txt 17     2026-11-20 08:15

String
58
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note the &lt;code&gt;String&lt;/code&gt;. Everything comes back as text, because CSV has no types. &lt;code&gt;Measure-Object&lt;/code&gt; happens to convert the values for you, but sorting does not: sorted as text, &lt;code&gt;"9"&lt;/code&gt; comes after &lt;code&gt;"26"&lt;/code&gt;. Cast when you sort:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="nc"&gt;Import&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Csv&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;files&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;csv&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="nc"&gt;Sort&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Property&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="nv"&gt;$_&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Length&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Descending&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="nc"&gt;Select&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;First&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;ExpandProperty&lt;/span&gt; &lt;span class="nc"&gt;Name&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.log
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you used &lt;code&gt;-Delimiter ';'&lt;/code&gt; on export, pass the same &lt;code&gt;-Delimiter ';'&lt;/code&gt; to &lt;code&gt;Import-Csv&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Safety: -NoClobber and -Force
&lt;/h2&gt;

&lt;p&gt;By default, Export-Csv overwrites an existing file without asking. When a report must not be replaced, add &lt;code&gt;-NoClobber&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;ChildItem&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="nc"&gt;Logs&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;File&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;
        &lt;span class="nc"&gt;Select&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Property&lt;/span&gt; &lt;span class="nc"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Length&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;
        &lt;span class="nc"&gt;Export&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Csv&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;files&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;csv&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;NoClobber&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;ErrorAction&lt;/span&gt; &lt;span class="nc"&gt;Stop&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"Refused: $($_.FullyQualifiedErrorId)"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Import&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Csv&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;files&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;csv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;LastWriteTime&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Refused: NoClobber,Microsoft.PowerShell.Commands.ExportCsvCommand
2026-11-20 08:15
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The original &lt;code&gt;files.csv&lt;/code&gt;, with its &lt;code&gt;LastWriteTime&lt;/code&gt; column, is untouched. In the other direction, &lt;code&gt;-Force&lt;/code&gt; lets Export-Csv overwrite a file marked &lt;strong&gt;read-only&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Item&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;files&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;excel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;csv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;IsReadOnly&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$true&lt;/span&gt;

&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;ChildItem&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="nc"&gt;Logs&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;File&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Select&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Property&lt;/span&gt; &lt;span class="nc"&gt;Name&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;
        &lt;span class="nc"&gt;Export&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Csv&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;files&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;excel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;csv&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;ErrorAction&lt;/span&gt; &lt;span class="nc"&gt;Stop&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s1"&gt;'Without -Force: write blocked'&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;ChildItem&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="nc"&gt;Logs&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;File&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Select&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Property&lt;/span&gt; &lt;span class="nc"&gt;Name&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="nc"&gt;Export&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Csv&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;files&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;excel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;csv&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Force&lt;/span&gt;
&lt;span class="s2"&gt;"With -Force: $((Get-Content -Path .\files-excel.csv)[0])"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Without -Force: write blocked
With -Force: "Name"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Read-only was protecting that file. &lt;code&gt;-Force&lt;/code&gt; removes the protection, so treat it like &lt;code&gt;-Confirm:$false&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Preview before you write: ConvertTo-Csv
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/convertto-csv" rel="noopener noreferrer"&gt;ConvertTo-Csv&lt;/a&gt; produces the same CSV text but sends it to the pipeline instead of a file, which makes it ideal for checking your columns. In PowerShell 7 you can also drop the quotes when they are not needed (&lt;code&gt;-UseQuotes&lt;/code&gt; is 7.0+ only):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;ChildItem&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="nc"&gt;Logs&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;File&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="nc"&gt;Select&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Property&lt;/span&gt; &lt;span class="nc"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Length&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="nc"&gt;ConvertTo&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Csv&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;UseQuotes&lt;/span&gt; &lt;span class="nc"&gt;AsNeeded&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Name,Length
app.log,26
error.log,15
report.txt,17
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  PowerShell 7.4 vs Windows PowerShell 5.1 cheat sheet
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Behavior&lt;/th&gt;
&lt;th&gt;PowerShell 7.4&lt;/th&gt;
&lt;th&gt;Windows PowerShell 5.1&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;#TYPE&lt;/code&gt; line&lt;/td&gt;
&lt;td&gt;Off by default (&lt;code&gt;-IncludeTypeInformation&lt;/code&gt; to add it)&lt;/td&gt;
&lt;td&gt;On by default (&lt;code&gt;-NoTypeInformation&lt;/code&gt; to remove it)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Default encoding&lt;/td&gt;
&lt;td&gt;&lt;code&gt;utf8NoBOM&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;ASCII&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;UTF-8 with BOM for Excel&lt;/td&gt;
&lt;td&gt;&lt;code&gt;-Encoding utf8BOM&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;-Encoding UTF8&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;-UseQuotes&lt;/code&gt;, &lt;code&gt;-QuoteFields&lt;/code&gt;, &lt;code&gt;-NoHeader&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Available&lt;/td&gt;
&lt;td&gt;Not available&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;-Append&lt;/code&gt;, &lt;code&gt;-Delimiter&lt;/code&gt;, &lt;code&gt;-UseCulture&lt;/code&gt;, &lt;code&gt;-NoClobber&lt;/code&gt;, &lt;code&gt;-Force&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Available&lt;/td&gt;
&lt;td&gt;Available&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Everything here is checked against the &lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/export-csv" rel="noopener noreferrer"&gt;Microsoft Learn Export-Csv reference&lt;/a&gt; for both versions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Practice it hands-on
&lt;/h2&gt;

&lt;p&gt;Export pipelines stick faster when you build them yourself. If you want somewhere to experiment without touching real reports, you can &lt;a href="https://windows-cli.arnost.org/en" rel="noopener noreferrer"&gt;practice PowerShell commands in a free browser terminal&lt;/a&gt; with CMD Master. It has interactive lessons for CMD, PowerShell and Bash.&lt;/p&gt;

&lt;h2&gt;
  
  
  Your turn: what's in the file?
&lt;/h2&gt;

&lt;p&gt;No error is thrown. What does &lt;code&gt;quiz.csv&lt;/code&gt; contain after this runs?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;pscustomobject&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;@&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;Name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'a.log'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nc"&gt;Size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="nc"&gt;Export&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Csv&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;quiz&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;csv&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;pscustomobject&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;@&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;Name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'b.log'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nc"&gt;Size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nc"&gt;Owner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'admin'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="nc"&gt;Export&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Csv&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;quiz&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;csv&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Append&lt;/span&gt;

&lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Content&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;quiz&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;csv&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Post your answer in the comments before you run it, and say what happened to &lt;code&gt;Owner&lt;/code&gt;. I'll reply with the real output and the rule behind it. If you'd rather test your guess first, &lt;a href="https://windows-cli.arnost.org/en" rel="noopener noreferrer"&gt;run it in the CMD Master interactive PowerShell terminal&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>powershell</category>
      <category>scripting</category>
      <category>windows</category>
      <category>beginners</category>
    </item>
    <item>
      <title>PowerShell Copy-Item Reports vs Reports\*: The Real Difference (and Why Your Backup Ends Up Nested)</title>
      <dc:creator>arnostorg</dc:creator>
      <pubDate>Fri, 20 Nov 2026 08:26:12 +0000</pubDate>
      <link>https://dev.to/arnostorg/powershell-copy-item-reports-vs-reports-the-real-difference-and-why-your-backup-ends-up-nested-20p</link>
      <guid>https://dev.to/arnostorg/powershell-copy-item-reports-vs-reports-the-real-difference-and-why-your-backup-ends-up-nested-20p</guid>
      <description>&lt;p&gt;Ever run a backup script twice and found &lt;code&gt;Backup\Reports\Reports&lt;/code&gt; sitting inside your backup? That isn't a bug in your script. It's how &lt;code&gt;Copy-Item -Recurse&lt;/code&gt; decides where a folder lands, and the answer depends on one thing: &lt;strong&gt;does the destination already exist?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This tip shows the exact difference between copying a &lt;strong&gt;folder&lt;/strong&gt; (&lt;code&gt;Reports&lt;/code&gt;) and copying its &lt;strong&gt;contents&lt;/strong&gt; (&lt;code&gt;Reports\*&lt;/code&gt;), why the same command gives two different results on run one and run two, and a three-line pattern that gives you the same tree every time.&lt;/p&gt;

&lt;h2&gt;
  
  
  The setup
&lt;/h2&gt;

&lt;p&gt;A small folder to copy:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="nc"&gt;New&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Item&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;ItemType&lt;/span&gt; &lt;span class="nc"&gt;Directory&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="nc"&gt;Reports&lt;/span&gt;\&lt;span class="mi"&gt;2026&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Force&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Out&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="kc"&gt;Null&lt;/span&gt;
&lt;span class="s1"&gt;'q1'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Content&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="nc"&gt;Reports&lt;/span&gt;\&lt;span class="mi"&gt;2026&lt;/span&gt;\&lt;span class="n"&gt;q1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;csv&lt;/span&gt;
&lt;span class="s1"&gt;'readme'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Content&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="nc"&gt;Reports&lt;/span&gt;\&lt;span class="n"&gt;readme&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;txt&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So &lt;code&gt;Reports&lt;/code&gt; holds &lt;code&gt;readme.txt&lt;/code&gt; and a subfolder &lt;code&gt;2026&lt;/code&gt; with &lt;code&gt;q1.csv&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Copy the folder: &lt;code&gt;-Path Reports&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Run this when &lt;code&gt;Backup&lt;/code&gt; does &lt;strong&gt;not&lt;/strong&gt; exist yet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="nc"&gt;Copy&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Item&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="nc"&gt;Reports&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Destination&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="nc"&gt;Backup&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Recurse&lt;/span&gt;
&lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;ChildItem&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="nc"&gt;Backup&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Recurse&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Name&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2026
readme.txt
2026\q1.csv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;PowerShell created &lt;code&gt;Backup&lt;/code&gt; and treated it as the &lt;strong&gt;new name&lt;/strong&gt; of the folder. That looks perfect, so the script goes into a scheduled task.&lt;/p&gt;

&lt;p&gt;Now run the exact same line again. &lt;code&gt;Backup&lt;/code&gt; exists this time:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="nc"&gt;Copy&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Item&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="nc"&gt;Reports&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Destination&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="nc"&gt;Backup&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Recurse&lt;/span&gt;
&lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;ChildItem&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="nc"&gt;Backup&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Recurse&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Name&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2026
Reports
readme.txt
2026\q1.csv
Reports\2026
Reports\readme.txt
Reports\2026\q1.csv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the destination is an existing folder, PowerShell copies the source folder &lt;strong&gt;into&lt;/strong&gt; it. Same command, different tree. That is where the "nested Reports" comes from.&lt;/p&gt;

&lt;p&gt;The rule:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Destination&lt;/th&gt;
&lt;th&gt;&lt;code&gt;Copy-Item -Path Reports -Destination Backup -Recurse&lt;/code&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;Backup&lt;/code&gt; does not exist&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;Backup&lt;/code&gt; becomes a copy of &lt;code&gt;Reports&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;Backup&lt;/code&gt; already exists&lt;/td&gt;
&lt;td&gt;You get &lt;code&gt;Backup\Reports\...&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Copy the contents: &lt;code&gt;-Path Reports\*&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The wildcard means "everything inside &lt;code&gt;Reports&lt;/code&gt;", not the folder itself. Copy into a folder that already exists, and the result does not change between runs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="nc"&gt;New&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Item&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;ItemType&lt;/span&gt; &lt;span class="nc"&gt;Directory&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="nc"&gt;Backup2&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Force&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Out&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="kc"&gt;Null&lt;/span&gt;
&lt;span class="nc"&gt;Copy&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Item&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="nc"&gt;Reports&lt;/span&gt;\&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Destination&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="nc"&gt;Backup2&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Recurse&lt;/span&gt;
&lt;span class="nc"&gt;Copy&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Item&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="nc"&gt;Reports&lt;/span&gt;\&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Destination&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="nc"&gt;Backup2&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Recurse&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Force&lt;/span&gt;
&lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;ChildItem&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="nc"&gt;Backup2&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Recurse&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Name&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output, after two runs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2026
readme.txt
2026\q1.csv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;-Force&lt;/code&gt; lets the second run overwrite files that are already there.&lt;/p&gt;

&lt;h3&gt;
  
  
  The trap inside the fix
&lt;/h3&gt;

&lt;p&gt;Skip the &lt;code&gt;New-Item&lt;/code&gt; step and the contents form gets weird. When I copied &lt;code&gt;Reports\*&lt;/code&gt; to a &lt;code&gt;Backup3&lt;/code&gt; that did not exist, the first item (the &lt;code&gt;2026&lt;/code&gt; folder) &lt;strong&gt;became&lt;/strong&gt; &lt;code&gt;Backup3&lt;/code&gt;, and the rest landed next to its files:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;q1.csv
readme.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;2026&lt;/code&gt; level is gone. So the contents form only works reliably when the destination folder exists first.&lt;/p&gt;

&lt;h2&gt;
  
  
  Forgetting &lt;code&gt;-Recurse&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Without &lt;code&gt;-Recurse&lt;/code&gt;, copying a folder gives you the folder and nothing in it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="nc"&gt;Copy&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Item&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="nc"&gt;Reports&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Destination&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="nc"&gt;NoRec&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;ChildItem&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="nc"&gt;NoRec&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Recurse&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Measure&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Count&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No error, no warning, just an empty folder. If a copy "worked" but the backup is empty, check for this first.&lt;/p&gt;

&lt;h2&gt;
  
  
  The pattern that gives the same tree every time
&lt;/h2&gt;

&lt;p&gt;Create the destination yourself, then copy the &lt;strong&gt;contents&lt;/strong&gt; into it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$source&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'C:\Data\Reports'&lt;/span&gt;
&lt;span class="nv"&gt;$dest&lt;/span&gt;   &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'D:\Backup\Reports'&lt;/span&gt;

&lt;span class="nc"&gt;New&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Item&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;ItemType&lt;/span&gt; &lt;span class="nc"&gt;Directory&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="nv"&gt;$dest&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Force&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Out&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="kc"&gt;Null&lt;/span&gt;
&lt;span class="nc"&gt;Copy&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Item&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Join&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="nv"&gt;$source&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;ChildPath&lt;/span&gt; &lt;span class="s1"&gt;'*'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Destination&lt;/span&gt; &lt;span class="nv"&gt;$dest&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Recurse&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Force&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;New-Item -Force&lt;/code&gt; creates the folder or does nothing if it already exists, with no error.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Join-Path ... '*'&lt;/code&gt; copies what is inside &lt;code&gt;Reports&lt;/code&gt;, never &lt;code&gt;Reports&lt;/code&gt; itself.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-Force&lt;/code&gt; overwrites older copies on later runs.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Run it once or fifty times and &lt;code&gt;D:\Backup\Reports&lt;/code&gt; looks the same. (It does not delete files you removed from the source. For a true mirror, use &lt;a href="https://dev.to/arnostorg/windows-robocopy-command-mirror-a-folder-without-deleting-the-wrong-files-dm0"&gt;Robocopy with /MIR, carefully&lt;/a&gt;.)&lt;/p&gt;

&lt;h2&gt;
  
  
  Preview before you copy: &lt;code&gt;-WhatIf&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;-WhatIf&lt;/code&gt; prints where the folder would really land, so you catch the nesting before it happens:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="nc"&gt;Copy&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Item&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="nc"&gt;Reports&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Destination&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="nc"&gt;Backup&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Recurse&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;WhatIf&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output when &lt;code&gt;Backup&lt;/code&gt; already exists:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;What if: Performing the operation "Copy Directory" on target "Item: C:\Work\Reports Destination: C:\Work\Backup\Reports".
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;See &lt;code&gt;Backup\Reports&lt;/code&gt; in the destination? That's the nested copy, caught before it happened.&lt;/p&gt;

&lt;h2&gt;
  
  
  Quick reference
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;You want&lt;/th&gt;
&lt;th&gt;Use&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;A one-time copy to a new folder name&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Copy-Item -Path .\Reports -Destination .\Backup -Recurse&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;A repeatable backup with the same tree every run&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;New-Item -ItemType Directory -Path $dest -Force&lt;/code&gt; then &lt;code&gt;Copy-Item -Path "$source\*" -Destination $dest -Recurse -Force&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;The folder itself inside an existing folder&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;Copy-Item -Path .\Reports -Destination .\Archive -Recurse&lt;/code&gt; (Archive exists)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;To check first&lt;/td&gt;
&lt;td&gt;Add &lt;code&gt;-WhatIf&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;I tested every block above in PowerShell 7.4.6. The demo ran on Linux with forward slashes; the behavior is the same with Windows paths. Docs: &lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/copy-item" rel="noopener noreferrer"&gt;Copy-Item on Microsoft Learn&lt;/a&gt;, &lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/new-item" rel="noopener noreferrer"&gt;New-Item&lt;/a&gt;, and &lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_wildcards" rel="noopener noreferrer"&gt;about_Wildcards&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Related tips: &lt;a href="https://dev.to/arnostorg/powershell-copy-item-duplicate-files-fast-and-safely-5846"&gt;Copy-Item basics for duplicating files&lt;/a&gt;, &lt;a href="https://dev.to/arnostorg/powershell-test-path-check-a-path-exists-before-you-act-3fn3"&gt;Test-Path checks before you act&lt;/a&gt;, and &lt;a href="https://dev.to/arnostorg/powershell-join-path-build-file-paths-without-slash-bugs-3jnb"&gt;Join-Path without slash bugs&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Practice it hands-on
&lt;/h2&gt;

&lt;p&gt;Folder-copy rules stick faster when you break them yourself. &lt;a href="https://windows-cli.arnost.org/en" rel="noopener noreferrer"&gt;CMD Master's free interactive PowerShell, CMD and Bash terminal lessons&lt;/a&gt; run in your browser, so you can practice file and folder commands without risking a real backup drive.&lt;/p&gt;

&lt;h2&gt;
  
  
  Your turn: what does this output?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Archive&lt;/code&gt; already exists and is empty. What does the last line print?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="nc"&gt;Copy&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Item&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="nc"&gt;Reports&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Destination&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="nc"&gt;Archive&lt;/span&gt;\&lt;span class="nc"&gt;Reports&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Recurse&lt;/span&gt;
&lt;span class="nc"&gt;Copy&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Item&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="nc"&gt;Reports&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Destination&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="nc"&gt;Archive&lt;/span&gt;\&lt;span class="nc"&gt;Reports&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Recurse&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;ChildItem&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="nc"&gt;Archive&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Recurse&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Directory&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Count&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Drop your number in the comments before you run it. I'll reply with the answer and why.&lt;/p&gt;

</description>
      <category>powershell</category>
      <category>scripting</category>
      <category>windows</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Windows XCOPY Command: Copy a Folder and All Its Subfolders</title>
      <dc:creator>arnostorg</dc:creator>
      <pubDate>Wed, 18 Nov 2026 08:21:35 +0000</pubDate>
      <link>https://dev.to/arnostorg/windows-xcopy-command-copy-a-folder-and-all-its-subfolders-2ib</link>
      <guid>https://dev.to/arnostorg/windows-xcopy-command-copy-a-folder-and-all-its-subfolders-2ib</guid>
      <description>&lt;p&gt;You want a copy of a project folder: every file, every subfolder, the empty &lt;code&gt;logs&lt;/code&gt; folder too. &lt;code&gt;COPY&lt;/code&gt; only handles files in one folder, so it is the wrong tool. &lt;code&gt;XCOPY&lt;/code&gt; copies a whole tree from a normal Command Prompt, with no extra install.&lt;/p&gt;

&lt;p&gt;Here is the one line most people need, then the flags that make it safe.&lt;/p&gt;

&lt;h2&gt;
  
  
  The command
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight batchfile"&gt;&lt;code&gt;&lt;span class="nb"&gt;xcopy&lt;/span&gt; &lt;span class="no"&gt;C&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;\&lt;span class="nc"&gt;Projects&lt;/span&gt;\&lt;span class="nc"&gt;WebApp&lt;/span&gt; &lt;span class="no"&gt;D&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;\&lt;span class="nc"&gt;Backup&lt;/span&gt;\&lt;span class="nc"&gt;WebApp&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What each flag does:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Flag&lt;/th&gt;
&lt;th&gt;What it does&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;/e&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Copies all subfolders, &lt;strong&gt;including empty ones&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;/i&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;If the destination doesn't exist, treat it as a folder (no prompt)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;/h&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Also copies hidden and system files&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;/y&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Overwrites existing files without asking&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The result: &lt;code&gt;D:\Backup\WebApp&lt;/code&gt; is created and gets the full tree from &lt;code&gt;C:\Projects\WebApp&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  /s vs /e: where empty folders go missing
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;/s&lt;/code&gt; copies subfolders &lt;strong&gt;unless they are empty&lt;/strong&gt;. &lt;code&gt;/e&lt;/code&gt; copies them even when they are empty.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight batchfile"&gt;&lt;code&gt;&lt;span class="k"&gt;rem&lt;/span&gt; &lt;span class="nc"&gt;Skips&lt;/span&gt; &lt;span class="n"&gt;empty&lt;/span&gt; &lt;span class="n"&gt;folders&lt;/span&gt; &lt;span class="n"&gt;like&lt;/span&gt; &lt;span class="n"&gt;logs&lt;/span&gt;\ &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;uploads&lt;/span&gt;\
&lt;span class="nb"&gt;xcopy&lt;/span&gt; &lt;span class="no"&gt;C&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;\&lt;span class="nc"&gt;Projects&lt;/span&gt;\&lt;span class="nc"&gt;WebApp&lt;/span&gt; &lt;span class="no"&gt;D&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;\&lt;span class="nc"&gt;Backup&lt;/span&gt;\&lt;span class="nc"&gt;WebApp&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;

&lt;span class="k"&gt;rem&lt;/span&gt; &lt;span class="nc"&gt;Keeps&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;full&lt;/span&gt; &lt;span class="n"&gt;folder&lt;/span&gt; &lt;span class="n"&gt;structure&lt;/span&gt;
&lt;span class="nb"&gt;xcopy&lt;/span&gt; &lt;span class="no"&gt;C&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;\&lt;span class="nc"&gt;Projects&lt;/span&gt;\&lt;span class="nc"&gt;WebApp&lt;/span&gt; &lt;span class="no"&gt;D&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;\&lt;span class="nc"&gt;Backup&lt;/span&gt;\&lt;span class="nc"&gt;WebApp&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If your app expects &lt;code&gt;logs\&lt;/code&gt; to exist at startup, the &lt;code&gt;/s&lt;/code&gt; copy can break it. Use &lt;code&gt;/e&lt;/code&gt; when you want an exact structure.&lt;/p&gt;

&lt;h2&gt;
  
  
  The "file or directory?" prompt
&lt;/h2&gt;

&lt;p&gt;Run XCOPY without &lt;code&gt;/i&lt;/code&gt; to a destination that doesn't exist yet, and it stops to ask:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Does D:\Backup\WebApp specify a file name
or directory name on the target
(F = file, D = directory)?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That prompt also freezes a scheduled task or batch file that nobody is watching. Two ways to avoid it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight batchfile"&gt;&lt;code&gt;&lt;span class="k"&gt;rem&lt;/span&gt; &lt;span class="nc"&gt;Option&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="n"&gt;says&lt;/span&gt; &lt;span class="s2"&gt;"it's a folder"&lt;/span&gt;
&lt;span class="nb"&gt;xcopy&lt;/span&gt; &lt;span class="no"&gt;C&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;\&lt;span class="nc"&gt;Projects&lt;/span&gt;\&lt;span class="nc"&gt;WebApp&lt;/span&gt; &lt;span class="no"&gt;D&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;\&lt;span class="nc"&gt;Backup&lt;/span&gt;\&lt;span class="nc"&gt;WebApp&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;

&lt;span class="k"&gt;rem&lt;/span&gt; &lt;span class="nc"&gt;Option&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;trailing&lt;/span&gt; &lt;span class="n"&gt;backslash&lt;/span&gt; &lt;span class="n"&gt;also&lt;/span&gt; &lt;span class="n"&gt;marks&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;folder&lt;/span&gt;
&lt;span class="nb"&gt;xcopy&lt;/span&gt; &lt;span class="no"&gt;C&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;\&lt;span class="nc"&gt;Projects&lt;/span&gt;\&lt;span class="nc"&gt;WebApp&lt;/span&gt; &lt;span class="no"&gt;D&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;\&lt;span class="nc"&gt;Backup&lt;/span&gt;\&lt;span class="nc"&gt;WebApp&lt;/span&gt;\ &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Preview first with /l
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;/l&lt;/code&gt; lists what would be copied without copying anything. Use it before a big copy:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight batchfile"&gt;&lt;code&gt;&lt;span class="nb"&gt;xcopy&lt;/span&gt; &lt;span class="no"&gt;C&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;\&lt;span class="nc"&gt;Projects&lt;/span&gt;\&lt;span class="nc"&gt;WebApp&lt;/span&gt; &lt;span class="no"&gt;D&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;\&lt;span class="nc"&gt;Backup&lt;/span&gt;\&lt;span class="nc"&gt;WebApp&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The last line shows the count, for example &lt;code&gt;152 File(s)&lt;/code&gt;. If that number looks wrong, fix the source path before you drop &lt;code&gt;/l&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Copy only what changed with /d
&lt;/h2&gt;

&lt;p&gt;Run the same backup tomorrow with &lt;code&gt;/d&lt;/code&gt;, and XCOPY copies only source files that are newer than the copy already in the destination:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight batchfile"&gt;&lt;code&gt;&lt;span class="nb"&gt;xcopy&lt;/span&gt; &lt;span class="no"&gt;C&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;\&lt;span class="nc"&gt;Projects&lt;/span&gt;\&lt;span class="nc"&gt;WebApp&lt;/span&gt; &lt;span class="no"&gt;D&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;\&lt;span class="nc"&gt;Backup&lt;/span&gt;\&lt;span class="nc"&gt;WebApp&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also give a date (&lt;code&gt;MM-DD-YYYY&lt;/code&gt; format) to copy files changed on or after it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight batchfile"&gt;&lt;code&gt;&lt;span class="nb"&gt;xcopy&lt;/span&gt; &lt;span class="no"&gt;C&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;\&lt;span class="nc"&gt;Projects&lt;/span&gt;\&lt;span class="nc"&gt;WebApp&lt;/span&gt; &lt;span class="no"&gt;D&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;\&lt;span class="nc"&gt;Backup&lt;/span&gt;\&lt;span class="nc"&gt;WebApp&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;01&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;2026&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Skip node_modules, .git, and build output with /exclude
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;/exclude&lt;/code&gt; takes a text file with one search string per line. Any file whose full path contains one of those strings is skipped.&lt;/p&gt;

&lt;p&gt;Create &lt;code&gt;C:\Scripts\skip.txt&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;\node_modules\
\.git\
\bin\
\obj\
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight batchfile"&gt;&lt;code&gt;&lt;span class="nb"&gt;xcopy&lt;/span&gt; &lt;span class="no"&gt;C&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;\&lt;span class="nc"&gt;Projects&lt;/span&gt;\&lt;span class="nc"&gt;WebApp&lt;/span&gt; &lt;span class="no"&gt;D&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;\&lt;span class="nc"&gt;Backup&lt;/span&gt;\&lt;span class="nc"&gt;WebApp&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;exclude&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="no"&gt;C&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;\&lt;span class="nc"&gt;Scripts&lt;/span&gt;\&lt;span class="n"&gt;skip&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;txt&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Watch out: the match is a plain substring of the whole path. A line that says just &lt;code&gt;obj&lt;/code&gt; also skips &lt;code&gt;objects.json&lt;/code&gt; and any folder named &lt;code&gt;objects&lt;/code&gt;. Wrapping the name in backslashes, as above, limits the match to a folder with that exact name.&lt;/p&gt;

&lt;h2&gt;
  
  
  Check the result in a batch file
&lt;/h2&gt;

&lt;p&gt;XCOPY returns an exit code, so a script can tell a good copy from a failed one:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Exit code&lt;/th&gt;
&lt;th&gt;Meaning&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;Files were copied without error&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;No files were found to copy&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;The user pressed Ctrl+C&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;Initialization error (bad path, bad syntax, not enough memory or disk space)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;Disk write error&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight batchfile"&gt;&lt;code&gt;&lt;span class="nd"&gt;@echo&lt;/span&gt; &lt;span class="n"&gt;off&lt;/span&gt;
&lt;span class="nb"&gt;xcopy&lt;/span&gt; &lt;span class="no"&gt;C&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;\&lt;span class="nc"&gt;Projects&lt;/span&gt;\&lt;span class="nc"&gt;WebApp&lt;/span&gt; &lt;span class="no"&gt;D&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;\&lt;span class="nc"&gt;Backup&lt;/span&gt;\&lt;span class="nc"&gt;WebApp&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;errorlevel&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nc"&gt;Backup&lt;/span&gt; &lt;span class="no"&gt;FAILED&lt;/span&gt; &lt;span class="n"&gt;with&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="n"&gt;errorlevel&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;
    &lt;span class="k"&gt;exit&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;errorlevel&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nc"&gt;Warning&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;exit&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="n"&gt;errorlevel&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;no&lt;/span&gt; &lt;span class="n"&gt;files&lt;/span&gt; &lt;span class="n"&gt;found&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cancelled&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nc"&gt;Backup&lt;/span&gt; &lt;span class="n"&gt;finished&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;if errorlevel 4&lt;/code&gt; is true for 4 &lt;strong&gt;or higher&lt;/strong&gt;, so check the biggest code first.&lt;/p&gt;

&lt;h2&gt;
  
  
  XCOPY or Robocopy?
&lt;/h2&gt;

&lt;p&gt;XCOPY is perfect for "copy this folder tree once" or a simple update copy. When you need retries on a flaky network share, multithreading, or a true mirror that also removes deleted files, use Robocopy. I covered that in &lt;a href="https://dev.to/arnostorg/windows-robocopy-command-mirror-a-folder-without-deleting-the-wrong-files-dm0"&gt;Windows Robocopy Command: Mirror a Folder Without Deleting the Wrong Files&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Related CMD tips:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/arnostorg/windows-copy-command-duplicate-files-fast-from-cmd-49i9"&gt;Windows COPY Command: Duplicate Files Fast From CMD&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/arnostorg/windows-mkdir-command-create-folders-fast-from-cmd-47c3"&gt;Windows MKDIR Command: Create Folders Fast From CMD&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/arnostorg/windows-rmdir-command-delete-a-folder-and-everything-inside-it-339b"&gt;Windows RMDIR Command: Delete a Folder and Everything Inside It&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Official reference: &lt;a href="https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/xcopy" rel="noopener noreferrer"&gt;xcopy on Microsoft Learn&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Practice it without risking real files
&lt;/h2&gt;

&lt;p&gt;Want to try &lt;code&gt;/s&lt;/code&gt; vs &lt;code&gt;/e&lt;/code&gt; without touching your own folders? &lt;a href="https://windows-cli.arnost.org/en" rel="noopener noreferrer"&gt;CMD Master's free interactive CMD lessons&lt;/a&gt; run in a browser terminal, so you can copy, break, and retry file commands safely. It also covers PowerShell and Bash.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick check:&lt;/strong&gt; a source folder has &lt;code&gt;readme.md&lt;/code&gt;, an empty &lt;code&gt;logs\&lt;/code&gt; folder, and a hidden &lt;code&gt;.env&lt;/code&gt; file. After &lt;code&gt;xcopy src dst /s /i&lt;/code&gt;, what ends up in &lt;code&gt;dst&lt;/code&gt;? Answer in the comments.&lt;/p&gt;

</description>
      <category>windows</category>
      <category>commandline</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>PowerShell -eq vs -contains: The Real Difference (and Why 'error.log' -contains 'log' Is False)</title>
      <dc:creator>arnostorg</dc:creator>
      <pubDate>Mon, 16 Nov 2026 08:28:03 +0000</pubDate>
      <link>https://dev.to/arnostorg/powershell-eq-vs-contains-the-real-difference-and-why-errorlog-contains-log-is-false-2i0d</link>
      <guid>https://dev.to/arnostorg/powershell-eq-vs-contains-the-real-difference-and-why-errorlog-contains-log-is-false-2i0d</guid>
      <description>&lt;p&gt;You wanted to know whether a file name contains &lt;code&gt;log&lt;/code&gt;. You wrote &lt;code&gt;'error.log' -contains 'log'&lt;/code&gt;, got &lt;code&gt;False&lt;/code&gt;, and started doubting your eyes. A few lines later, &lt;code&gt;if ($values -eq 0)&lt;/code&gt; skipped a list that clearly has a zero in it.&lt;/p&gt;

&lt;p&gt;Both bugs come from the same misunderstanding. &lt;code&gt;-contains&lt;/code&gt; is &lt;strong&gt;not&lt;/strong&gt; a substring check. It asks "is this exact item in this collection?" And &lt;code&gt;-eq&lt;/code&gt; with an array on the left does &lt;strong&gt;not&lt;/strong&gt; return True or False. It returns the matching items, and that result can be falsy even when a match exists.&lt;/p&gt;

&lt;p&gt;Everything below was run in PowerShell 7.4 (7.4.6), and the output shown is the real output. The same rules apply in Windows PowerShell 5.1.&lt;/p&gt;

&lt;h2&gt;
  
  
  The shortest honest comparison
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;-eq&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;-contains&lt;/code&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Question it asks&lt;/td&gt;
&lt;td&gt;"Is this equal to that?"&lt;/td&gt;
&lt;td&gt;"Is this item in the collection?"&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Scalar on the left&lt;/td&gt;
&lt;td&gt;Returns &lt;code&gt;True&lt;/code&gt; / &lt;code&gt;False&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Treats the left side as a one-item collection&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Array on the left&lt;/td&gt;
&lt;td&gt;Returns the &lt;strong&gt;matching items&lt;/strong&gt; (a filter)&lt;/td&gt;
&lt;td&gt;Returns &lt;code&gt;True&lt;/code&gt; / &lt;code&gt;False&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Substring check&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;No (use &lt;code&gt;-like&lt;/code&gt; or &lt;code&gt;.Contains()&lt;/code&gt;)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Wildcards&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Case-sensitive version&lt;/td&gt;
&lt;td&gt;&lt;code&gt;-ceq&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;-ccontains&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Mirror operator&lt;/td&gt;
&lt;td&gt;(none)&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;-in&lt;/code&gt; (item first, collection second)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Trap 1: &lt;code&gt;-contains&lt;/code&gt; is not "string contains"
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="s1"&gt;'error.log'&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;contains&lt;/span&gt; &lt;span class="s1"&gt;'log'&lt;/span&gt;
&lt;span class="s1"&gt;'error.log'&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'log'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="s1"&gt;'error.log'&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;like&lt;/span&gt; &lt;span class="s1"&gt;'*log*'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;False
True
True
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;'error.log'&lt;/code&gt; is a single string, so PowerShell treats it as a collection with one item: &lt;code&gt;'error.log'&lt;/code&gt;. Is the item &lt;code&gt;'log'&lt;/code&gt; in that collection? No. For "does this text include that text", use the string method &lt;code&gt;.Contains()&lt;/code&gt; (case-sensitive) or &lt;code&gt;-like&lt;/code&gt; with stars on both sides (case-insensitive).&lt;/p&gt;

&lt;h2&gt;
  
  
  What &lt;code&gt;-contains&lt;/code&gt; is actually for
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;-contains&lt;/code&gt; checks membership in a list, item by item, using the same equality rules as &lt;code&gt;-eq&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$allowed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'.log'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'.txt'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'.csv'&lt;/span&gt;

&lt;span class="nv"&gt;$allowed&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;contains&lt;/span&gt; &lt;span class="s1"&gt;'.txt'&lt;/span&gt;
&lt;span class="nv"&gt;$allowed&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;contains&lt;/span&gt; &lt;span class="s1"&gt;'.TXT'&lt;/span&gt;
&lt;span class="nv"&gt;$allowed&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;ccontains&lt;/span&gt; &lt;span class="s1"&gt;'.TXT'&lt;/span&gt;
&lt;span class="nv"&gt;$allowed&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;contains&lt;/span&gt; &lt;span class="s1"&gt;'*.txt'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;True
True
False
False
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notes on the four results:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;.txt&lt;/code&gt; is in the list, so &lt;code&gt;True&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.TXT&lt;/code&gt; is also &lt;code&gt;True&lt;/code&gt;, because &lt;code&gt;-contains&lt;/code&gt; ignores case by default.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-ccontains&lt;/code&gt; is the case-sensitive version, so &lt;code&gt;.TXT&lt;/code&gt; is not found.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;*.txt&lt;/code&gt; is &lt;code&gt;False&lt;/code&gt;. There are no wildcards here. The star is just a character.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Trap 2: &lt;code&gt;-eq&lt;/code&gt; on an array is a filter
&lt;/h2&gt;

&lt;p&gt;Put an array on the left of &lt;code&gt;-eq&lt;/code&gt; and you don't get a Boolean back. You get every item that is equal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$codes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;404&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;

&lt;span class="nv"&gt;$codes&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;eq&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;
&lt;span class="s1"&gt;'---'&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$codes&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;eq&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Count&lt;/span&gt;
&lt;span class="s1"&gt;'---'&lt;/span&gt;
&lt;span class="nv"&gt;$codes&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;contains&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;200
200
---
2
---
True
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;$codes -eq 200&lt;/code&gt; returned both &lt;code&gt;200&lt;/code&gt; values. That is useful when you want the matches (or a count of them). When you only want a yes/no answer, &lt;code&gt;-contains&lt;/code&gt; gives you a clean &lt;code&gt;True&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Trap 3: &lt;code&gt;if ($array -eq ...)&lt;/code&gt; can be false even when it matches
&lt;/h2&gt;

&lt;p&gt;This one causes real bugs in scripts. Read the conditions and guess the output first:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$values&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$values&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;eq&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s1"&gt;'found zero'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s1"&gt;'no zero?'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nv"&gt;$names&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'alice'&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$names&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;eq&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s1"&gt;'found empty name'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s1"&gt;'no empty name?'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;no zero?
no empty name?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both lists contain the value, and both &lt;code&gt;if&lt;/code&gt; statements still take the &lt;code&gt;else&lt;/code&gt; branch. Here is why:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;$values -eq 0&lt;/code&gt; returns a one-item array: &lt;code&gt;@(0)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;PowerShell converts a one-item array to Boolean by looking at that item. &lt;code&gt;0&lt;/code&gt; is falsy, so the whole condition is &lt;code&gt;False&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The same happens with &lt;code&gt;''&lt;/code&gt;: a one-item array holding an empty string is falsy.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Switch to &lt;code&gt;-contains&lt;/code&gt; and the answer is what you meant:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$values&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$values&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;contains&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s1"&gt;'found zero'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s1"&gt;'no zero'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nv"&gt;$names&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'alice'&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$names&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;contains&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s1"&gt;'found empty name'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s1"&gt;'no empty name'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;found zero
found empty name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Trap 4: always put &lt;code&gt;$null&lt;/code&gt; on the left
&lt;/h2&gt;

&lt;p&gt;Comparing with &lt;code&gt;$null&lt;/code&gt; has the same filter behavior, just with the opposite surprise:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$null&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$list&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;eq&lt;/span&gt; &lt;span class="nv"&gt;$null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s1"&gt;'list is null'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s1"&gt;'list is not null'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$null&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;eq&lt;/span&gt; &lt;span class="nv"&gt;$list&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s1"&gt;'list is null'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s1"&gt;'list is not null'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="s1"&gt;'---'&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$list&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;eq&lt;/span&gt; &lt;span class="nv"&gt;$null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Count&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;list is null
list is not null
---
2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;$list -eq $null&lt;/code&gt; returned the two &lt;code&gt;$null&lt;/code&gt; items. A two-item array is truthy, so the first &lt;code&gt;if&lt;/code&gt; claims the whole list "is null". &lt;code&gt;$null -eq $list&lt;/code&gt; asks the question you meant ("is &lt;code&gt;$list&lt;/code&gt; itself null?") and correctly says no. This is why PSScriptAnalyzer flags &lt;code&gt;$var -eq $null&lt;/code&gt; and recommends &lt;code&gt;$null -eq $var&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;-in&lt;/code&gt; and &lt;code&gt;-notin&lt;/code&gt;: the same check, flipped
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;-in&lt;/code&gt; is &lt;code&gt;-contains&lt;/code&gt; with the operands swapped. Many people find it reads more naturally in a &lt;code&gt;Where-Object&lt;/code&gt; filter:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$allowed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'.log'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'.txt'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'.csv'&lt;/span&gt;

&lt;span class="s1"&gt;'.txt'&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nv"&gt;$allowed&lt;/span&gt;
&lt;span class="s1"&gt;'.exe'&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nv"&gt;$allowed&lt;/span&gt;
&lt;span class="s1"&gt;'.exe'&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;notin&lt;/span&gt; &lt;span class="nv"&gt;$allowed&lt;/span&gt;
&lt;span class="nv"&gt;$allowed&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;notcontains&lt;/span&gt; &lt;span class="s1"&gt;'.exe'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;True
False
True
True
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;'.txt' -in $allowed&lt;/code&gt; and &lt;code&gt;$allowed -contains '.txt'&lt;/code&gt; mean the same thing. Pick whichever order reads better in your script, and stay consistent.&lt;/p&gt;

&lt;h2&gt;
  
  
  Type conversion follows the left side
&lt;/h2&gt;

&lt;p&gt;PowerShell converts the right-hand value to the type of the left-hand value before comparing. With &lt;code&gt;-contains&lt;/code&gt;, each item in the collection plays the "left" role:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;contains&lt;/span&gt; &lt;span class="s1"&gt;'2'&lt;/span&gt;
&lt;span class="s1"&gt;'1'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'2'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'3'&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;contains&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="s1"&gt;'---'&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;eq&lt;/span&gt; &lt;span class="s1"&gt;'01'&lt;/span&gt;
&lt;span class="s1"&gt;'01'&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;eq&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;True
True
---
True
False
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;1 -eq '01'&lt;/code&gt; is &lt;code&gt;True&lt;/code&gt; because &lt;code&gt;'01'&lt;/code&gt; is converted to the number &lt;code&gt;1&lt;/code&gt;. &lt;code&gt;'01' -eq 1&lt;/code&gt; is &lt;code&gt;False&lt;/code&gt; because &lt;code&gt;1&lt;/code&gt; becomes the string &lt;code&gt;'1'&lt;/code&gt;, which isn't &lt;code&gt;'01'&lt;/code&gt;. If you compare IDs or version-like strings, make sure both sides have the type you expect.&lt;/p&gt;

&lt;h2&gt;
  
  
  Real use: an extension allow list with &lt;code&gt;Get-ChildItem&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Here is a practical script that filters files by an allow list and checks that a required file exists. The demo creates its own temp folder and cleans up after itself:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$dir&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Join&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;IO&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nc"&gt;GetTempPath&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="s1"&gt;'eq-vs-contains-demo'&lt;/span&gt;
&lt;span class="nc"&gt;New&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Item&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;ItemType&lt;/span&gt; &lt;span class="nc"&gt;Directory&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="nv"&gt;$dir&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Force&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Out&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="kc"&gt;Null&lt;/span&gt;
&lt;span class="s1"&gt;'app.log'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'notes.txt'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'setup.exe'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'data.CSV'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'script.ps1'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="k"&gt;ForEach&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;New&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Item&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="nv"&gt;$dir&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Name&lt;/span&gt; &lt;span class="nv"&gt;$_&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;ItemType&lt;/span&gt; &lt;span class="nc"&gt;File&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Force&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Out&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="kc"&gt;Null&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nv"&gt;$allowed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'.log'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'.txt'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'.csv'&lt;/span&gt;

&lt;span class="c1"&gt;# Keep only files whose extension is on the allow list&lt;/span&gt;
&lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;ChildItem&lt;/span&gt; &lt;span class="nv"&gt;$dir&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;File&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="nc"&gt;Where&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;$allowed&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;contains&lt;/span&gt; &lt;span class="nv"&gt;$_&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Extension&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="nc"&gt;Select&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;ExpandProperty&lt;/span&gt; &lt;span class="nc"&gt;Name&lt;/span&gt;
&lt;span class="s1"&gt;'---'&lt;/span&gt;
&lt;span class="c1"&gt;# Check a required file exists before you continue&lt;/span&gt;
&lt;span class="nv"&gt;$names&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;ChildItem&lt;/span&gt; &lt;span class="nv"&gt;$dir&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;File&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Name&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$names&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;contains&lt;/span&gt; &lt;span class="s1"&gt;'config.json'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s1"&gt;'config found'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s1"&gt;'config.json is missing'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;Remove&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Item&lt;/span&gt; &lt;span class="nv"&gt;$dir&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Recurse&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Force&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.log
data.CSV
notes.txt
---
config.json is missing
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;data.CSV&lt;/code&gt; passes because &lt;code&gt;-contains&lt;/code&gt; ignores case, and &lt;code&gt;setup.exe&lt;/code&gt; and &lt;code&gt;script.ps1&lt;/code&gt; are filtered out. The second check gives a clean yes/no about a required file without any substring surprises.&lt;/p&gt;

&lt;p&gt;If you want to try these checks without setting anything up, &lt;a href="https://windows-cli.arnost.org/en" rel="noopener noreferrer"&gt;CMD Master's free interactive PowerShell course in the browser&lt;/a&gt; gives you a real terminal to experiment in.&lt;/p&gt;

&lt;h2&gt;
  
  
  Quick checklist
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;"Is this item in my list?": use &lt;code&gt;-contains&lt;/code&gt; or &lt;code&gt;-in&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;"Does this text include that text?": use &lt;code&gt;-like '*text*'&lt;/code&gt; or &lt;code&gt;.Contains()&lt;/code&gt;, never &lt;code&gt;-contains&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;"Which items match?": use &lt;code&gt;-eq&lt;/code&gt; with the array on the left, or &lt;code&gt;Where-Object&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Inside an &lt;code&gt;if&lt;/code&gt;, avoid &lt;code&gt;$array -eq $value&lt;/code&gt;. A single &lt;code&gt;0&lt;/code&gt;, &lt;code&gt;''&lt;/code&gt;, or &lt;code&gt;$null&lt;/code&gt; match is falsy.&lt;/li&gt;
&lt;li&gt;Comparing with &lt;code&gt;$null&lt;/code&gt;: write &lt;code&gt;$null -eq $var&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Need case to matter: &lt;code&gt;-ceq&lt;/code&gt; / &lt;code&gt;-ccontains&lt;/code&gt; / &lt;code&gt;-cin&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Mixed types: the left side decides the conversion.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Practice in a real terminal
&lt;/h2&gt;

&lt;p&gt;Comparison operators stick much better once you type them yourself and watch the output. &lt;a href="https://windows-cli.arnost.org/en" rel="noopener noreferrer"&gt;CMD Master&lt;/a&gt; is a free, interactive browser terminal for learning CMD, PowerShell, and Bash, with instant feedback on every command and nothing to install.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sources:&lt;/strong&gt; Microsoft Learn: &lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_comparison_operators" rel="noopener noreferrer"&gt;about_Comparison_Operators&lt;/a&gt;, &lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_booleans" rel="noopener noreferrer"&gt;about_Booleans&lt;/a&gt;, &lt;a href="https://learn.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-null" rel="noopener noreferrer"&gt;Everything you wanted to know about $null&lt;/a&gt;, &lt;a href="https://learn.microsoft.com/en-us/powershell/utility-modules/psscriptanalyzer/rules/possibleincorrectcomparisonwithnull" rel="noopener noreferrer"&gt;PSPossibleIncorrectComparisonWithNull rule&lt;/a&gt;, &lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/where-object" rel="noopener noreferrer"&gt;Where-Object&lt;/a&gt;, &lt;a href="https://learn.microsoft.com/en-us/dotnet/api/system.string.contains" rel="noopener noreferrer"&gt;String.Contains&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Quick quiz
&lt;/h2&gt;

&lt;p&gt;Without running it: what does this output in PowerShell 7.4?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$tags&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'prod'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Prod'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'dev'&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$tags&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;eq&lt;/span&gt; &lt;span class="s1"&gt;'PROD'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Count&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Is it &lt;code&gt;0&lt;/code&gt;, &lt;code&gt;1&lt;/code&gt;, &lt;code&gt;2&lt;/code&gt;, or something else? Drop your answer (and why) in the comments.&lt;/p&gt;

</description>
      <category>powershell</category>
      <category>scripting</category>
      <category>windows</category>
      <category>beginners</category>
    </item>
    <item>
      <title>PowerShell -like vs -match: The Real Difference (and Why Your Filter Matches Too Much)</title>
      <dc:creator>arnostorg</dc:creator>
      <pubDate>Fri, 13 Nov 2026 08:29:45 +0000</pubDate>
      <link>https://dev.to/arnostorg/powershell-like-vs-match-the-real-difference-and-why-your-filter-matches-too-much-2gf1</link>
      <guid>https://dev.to/arnostorg/powershell-like-vs-match-the-real-difference-and-why-your-filter-matches-too-much-2gf1</guid>
      <description>&lt;p&gt;You wanted only the &lt;code&gt;.log&lt;/code&gt; files. You wrote &lt;code&gt;Where-Object Name -match 'log'&lt;/code&gt;, and back came &lt;code&gt;catalog.txt&lt;/code&gt;, &lt;code&gt;backup.log.zip&lt;/code&gt;, and &lt;code&gt;error-log.txt&lt;/code&gt; too. No error, just too many results.&lt;/p&gt;

&lt;p&gt;The bug is mixing up two operators that look alike. &lt;code&gt;-like&lt;/code&gt; uses &lt;strong&gt;wildcards&lt;/strong&gt; and has to match the &lt;strong&gt;whole string&lt;/strong&gt;. &lt;code&gt;-match&lt;/code&gt; uses &lt;strong&gt;regular expressions&lt;/strong&gt; and is happy to match &lt;strong&gt;any part&lt;/strong&gt; of the string. Once that clicks, most "my filter matches too much" problems go away.&lt;/p&gt;

&lt;p&gt;Everything below was run in PowerShell 7.4 (7.4.6), and the output shown is the real output. The same rules apply in Windows PowerShell 5.1.&lt;/p&gt;

&lt;h2&gt;
  
  
  The shortest honest comparison
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;-like&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;-match&lt;/code&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Pattern type&lt;/td&gt;
&lt;td&gt;Wildcard (&lt;code&gt;*&lt;/code&gt;, &lt;code&gt;?&lt;/code&gt;, &lt;code&gt;[ ]&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;Regular expression (regex)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;How much must match&lt;/td&gt;
&lt;td&gt;The &lt;strong&gt;whole&lt;/strong&gt; string&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;Any part&lt;/strong&gt; of the string&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;.&lt;/code&gt; means&lt;/td&gt;
&lt;td&gt;A literal dot&lt;/td&gt;
&lt;td&gt;Any single character&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Anchors needed&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Yes, &lt;code&gt;^&lt;/code&gt; and &lt;code&gt;$&lt;/code&gt; if you want start/end&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sets &lt;code&gt;$Matches&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Yes (for a single string)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Case-sensitive version&lt;/td&gt;
&lt;td&gt;&lt;code&gt;-clike&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;-cmatch&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Negated version&lt;/td&gt;
&lt;td&gt;&lt;code&gt;-notlike&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;-notmatch&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  The trap in three lines
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="s1"&gt;'catalog.txt'&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;like&lt;/span&gt; &lt;span class="s1"&gt;'log'&lt;/span&gt;
&lt;span class="s1"&gt;'catalog.txt'&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;like&lt;/span&gt; &lt;span class="s1"&gt;'*log*'&lt;/span&gt;
&lt;span class="s1"&gt;'catalog.txt'&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;match&lt;/span&gt; &lt;span class="s1"&gt;'log'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;False
True
True
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;-like 'log'&lt;/code&gt; asks "is the whole string exactly &lt;code&gt;log&lt;/code&gt;?" No. To get a "contains" check with &lt;code&gt;-like&lt;/code&gt;, you need stars on both sides. &lt;code&gt;-match 'log'&lt;/code&gt; asks "does &lt;code&gt;log&lt;/code&gt; appear anywhere?" Yes, inside &lt;code&gt;cata&lt;/code&gt;&lt;strong&gt;&lt;code&gt;log&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why &lt;code&gt;-match '.log'&lt;/code&gt; matches almost everything
&lt;/h2&gt;

&lt;p&gt;Many people translate &lt;code&gt;*.log&lt;/code&gt; into regex by just dropping the star. Here is what happens:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$names&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'app.log'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'catalog.txt'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'backup.log.zip'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'error-log.txt'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'applog'&lt;/span&gt;

&lt;span class="nv"&gt;$names&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;like&lt;/span&gt; &lt;span class="s1"&gt;'*.log'&lt;/span&gt;
&lt;span class="s1"&gt;'---'&lt;/span&gt;
&lt;span class="nv"&gt;$names&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;match&lt;/span&gt; &lt;span class="s1"&gt;'.log'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.log
---
app.log
catalog.txt
backup.log.zip
error-log.txt
applog
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;-like '*.log'&lt;/code&gt; gave exactly one file. &lt;code&gt;-match '.log'&lt;/code&gt; matched all five, because in regex:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;.&lt;/code&gt; is not a dot. It means "any one character", so &lt;code&gt;alog&lt;/code&gt; in &lt;code&gt;catalog.txt&lt;/code&gt; and &lt;code&gt;plog&lt;/code&gt; in &lt;code&gt;applog&lt;/code&gt; both count.&lt;/li&gt;
&lt;li&gt;There is no anchor, so the match can sit anywhere, like the middle of &lt;code&gt;backup.log.zip&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The regex that means the same thing as &lt;code&gt;*.log&lt;/code&gt; escapes the dot and anchors the end:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$names&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'app.log'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'catalog.txt'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'backup.log.zip'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'error-log.txt'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'applog'&lt;/span&gt;

&lt;span class="nv"&gt;$names&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;match&lt;/span&gt; &lt;span class="s1"&gt;'\.log$'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.log
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;\.&lt;/code&gt; is a literal dot, and &lt;code&gt;$&lt;/code&gt; means "end of string". If you also want "must start with", add &lt;code&gt;^&lt;/code&gt; at the front, for example &lt;code&gt;'^app\.log$'&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wildcards cheat sheet for &lt;code&gt;-like&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Wildcards only have three special characters:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;*&lt;/code&gt; is zero or more characters&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;?&lt;/code&gt; is exactly one character&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[ ]&lt;/code&gt; is one character from a set or range, like &lt;code&gt;[abc]&lt;/code&gt; or &lt;code&gt;[0-5]&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="s1"&gt;'report1.csv'&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;like&lt;/span&gt; &lt;span class="s1"&gt;'report?.csv'&lt;/span&gt;
&lt;span class="s1"&gt;'report10.csv'&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;like&lt;/span&gt; &lt;span class="s1"&gt;'report?.csv'&lt;/span&gt;
&lt;span class="s1"&gt;'report7.csv'&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;like&lt;/span&gt; &lt;span class="s1"&gt;'report[0-5].csv'&lt;/span&gt;
&lt;span class="s1"&gt;'server-b'&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;like&lt;/span&gt; &lt;span class="s1"&gt;'server-[abc]'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;True
False
False
True
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;report10.csv&lt;/code&gt; fails because &lt;code&gt;?&lt;/code&gt; is exactly one character, and &lt;code&gt;10&lt;/code&gt; is two. &lt;code&gt;report7.csv&lt;/code&gt; fails because &lt;code&gt;7&lt;/code&gt; isn't in &lt;code&gt;[0-5]&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;-match&lt;/code&gt; gives you &lt;code&gt;$Matches&lt;/code&gt; for free
&lt;/h2&gt;

&lt;p&gt;When &lt;code&gt;-match&lt;/code&gt; runs against a single string and finds something, it fills the &lt;code&gt;$Matches&lt;/code&gt; automatic variable. Index &lt;code&gt;0&lt;/code&gt; is the whole match, and each pair of parentheses becomes a numbered group:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="s1"&gt;'srv-web-01'&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;match&lt;/span&gt; &lt;span class="s1"&gt;'web-(\d+)'&lt;/span&gt;
&lt;span class="nv"&gt;$Matches&lt;/span&gt;
&lt;span class="s1"&gt;'---'&lt;/span&gt;
&lt;span class="nv"&gt;$Matches&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;True

Name                           Value
----                           -----
1                              01
0                              web-01
---
01
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Named groups are easier to read in scripts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="s1"&gt;'user=alice; role=admin'&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;match&lt;/span&gt; &lt;span class="s1"&gt;'user=(?&amp;lt;user&amp;gt;\w+).*role=(?&amp;lt;role&amp;gt;\w+)'&lt;/span&gt;
&lt;span class="s2"&gt;"$($Matches.user) is $($Matches.role)"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;True
alice is admin
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;-like&lt;/code&gt; never touches &lt;code&gt;$Matches&lt;/code&gt;. It only tells you True or False.&lt;/p&gt;

&lt;h2&gt;
  
  
  On arrays, both operators filter (and &lt;code&gt;$Matches&lt;/code&gt; lies)
&lt;/h2&gt;

&lt;p&gt;Put an array on the left and both operators stop returning True/False. They return the items that match. That's handy, but there's a catch: &lt;code&gt;-match&lt;/code&gt; on an array does &lt;strong&gt;not&lt;/strong&gt; update &lt;code&gt;$Matches&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$null&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'warm-up'&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;match&lt;/span&gt; &lt;span class="s1"&gt;'warm'&lt;/span&gt;   &lt;span class="c1"&gt;# sets $Matches&lt;/span&gt;
&lt;span class="nv"&gt;$servers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'srv-web-01'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'srv-db-02'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'srv-web-03'&lt;/span&gt;

&lt;span class="nv"&gt;$servers&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;match&lt;/span&gt; &lt;span class="s1"&gt;'web'&lt;/span&gt;
&lt;span class="s1"&gt;'---'&lt;/span&gt;
&lt;span class="nv"&gt;$Matches&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;               &lt;span class="c1"&gt;# still the old value&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;srv-web-01
srv-web-03
---
warm
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The filtering worked, but &lt;code&gt;$Matches[0]&lt;/code&gt; is still &lt;code&gt;warm&lt;/code&gt; from the earlier single-string match. If you need capture groups for each item, loop over the items and run &lt;code&gt;-match&lt;/code&gt; on one string at a time, or use &lt;code&gt;Select-String&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Case-insensitive by default
&lt;/h2&gt;

&lt;p&gt;Both operators ignore case unless you ask for the &lt;code&gt;c&lt;/code&gt; version:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="s1"&gt;'ERROR.LOG'&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;like&lt;/span&gt; &lt;span class="s1"&gt;'*.log'&lt;/span&gt;
&lt;span class="s1"&gt;'ERROR.LOG'&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;clike&lt;/span&gt; &lt;span class="s1"&gt;'*.log'&lt;/span&gt;
&lt;span class="s1"&gt;'ERROR: disk full'&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;match&lt;/span&gt; &lt;span class="s1"&gt;'error'&lt;/span&gt;
&lt;span class="s1"&gt;'ERROR: disk full'&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;cmatch&lt;/span&gt; &lt;span class="s1"&gt;'error'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;True
False
True
False
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are also &lt;code&gt;-ilike&lt;/code&gt; and &lt;code&gt;-imatch&lt;/code&gt;, which spell out the default case-insensitive behavior.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;-notlike&lt;/code&gt; and &lt;code&gt;-notmatch&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The negated versions follow the same rules, so the same anchoring advice applies:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$names&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'app.log'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'catalog.txt'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'backup.log.zip'&lt;/span&gt;

&lt;span class="nv"&gt;$names&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;notlike&lt;/span&gt; &lt;span class="s1"&gt;'*.log'&lt;/span&gt;
&lt;span class="s1"&gt;'---'&lt;/span&gt;
&lt;span class="nv"&gt;$names&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;notmatch&lt;/span&gt; &lt;span class="s1"&gt;'\.log$'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;catalog.txt
backup.log.zip
---
catalog.txt
backup.log.zip
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Without the anchor, &lt;code&gt;-notmatch 'log'&lt;/code&gt; would throw away &lt;code&gt;catalog.txt&lt;/code&gt; too, which is the same trap in reverse.&lt;/p&gt;

&lt;h2&gt;
  
  
  Escaping user input: &lt;code&gt;[WildcardPattern]::Escape&lt;/code&gt; and &lt;code&gt;[regex]::Escape&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;If the pattern comes from a variable (a file name, a search box, a CSV column), special characters will bite you. Square brackets are wildcards for &lt;code&gt;-like&lt;/code&gt;. Dots, dollar signs, and parentheses are regex for &lt;code&gt;-match&lt;/code&gt;. Escape the text first:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="s1"&gt;'file[1].txt'&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;like&lt;/span&gt; &lt;span class="s1"&gt;'file[1].txt'&lt;/span&gt;
&lt;span class="s1"&gt;'file[1].txt'&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;like&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;WildcardPattern&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nc"&gt;Escape&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'file[1].txt'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;WildcardPattern&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nc"&gt;Escape&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'file[1].txt'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="s1"&gt;'---'&lt;/span&gt;
&lt;span class="s1"&gt;'price: $5.00 (USD)'&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;match&lt;/span&gt; &lt;span class="s1"&gt;'$5.00 (USD)'&lt;/span&gt;
&lt;span class="s1"&gt;'price: $5.00 (USD)'&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;match&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nc"&gt;Escape&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'$5.00 (USD)'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nc"&gt;Escape&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'$5.00 (USD)'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;False
True
file`[1`].txt
---
False
True
\$5\.00\ \(USD\)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;file[1].txt&lt;/code&gt; treats &lt;code&gt;[1]&lt;/code&gt; as "one character from the set &lt;code&gt;1&lt;/code&gt;", so it doesn't match the literal brackets. &lt;code&gt;'$5.00 (USD)'&lt;/code&gt; as a regex means "end of string, then 5, any character, 00, space, a group containing USD", which can never match. After escaping, both behave like plain text.&lt;/p&gt;

&lt;h2&gt;
  
  
  Real use: filtering &lt;code&gt;Get-ChildItem&lt;/code&gt; with &lt;code&gt;Where-Object&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Here is the original bug and two correct fixes. The demo creates its own temp folder and cleans up after itself:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$dir&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Join&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;IO&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nc"&gt;GetTempPath&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="s1"&gt;'like-vs-match-demo'&lt;/span&gt;
&lt;span class="nc"&gt;New&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Item&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;ItemType&lt;/span&gt; &lt;span class="nc"&gt;Directory&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="nv"&gt;$dir&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Force&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Out&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="kc"&gt;Null&lt;/span&gt;
&lt;span class="s1"&gt;'app.log'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'catalog.txt'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'backup.log.zip'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'error-log.txt'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="k"&gt;ForEach&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;New&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Item&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="nv"&gt;$dir&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Name&lt;/span&gt; &lt;span class="nv"&gt;$_&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;ItemType&lt;/span&gt; &lt;span class="nc"&gt;File&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Force&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Out&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="kc"&gt;Null&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;# Too much: regex 'log' matches anywhere in the name&lt;/span&gt;
&lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;ChildItem&lt;/span&gt; &lt;span class="nv"&gt;$dir&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Where&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="nc"&gt;Name&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;match&lt;/span&gt; &lt;span class="s1"&gt;'log'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Select&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;ExpandProperty&lt;/span&gt; &lt;span class="nc"&gt;Name&lt;/span&gt;
&lt;span class="s1"&gt;'---'&lt;/span&gt;
&lt;span class="c1"&gt;# Exactly .log files: wildcard...&lt;/span&gt;
&lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;ChildItem&lt;/span&gt; &lt;span class="nv"&gt;$dir&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Where&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="nc"&gt;Name&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;like&lt;/span&gt; &lt;span class="s1"&gt;'*.log'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Select&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;ExpandProperty&lt;/span&gt; &lt;span class="nc"&gt;Name&lt;/span&gt;
&lt;span class="c1"&gt;# ...or anchored regex&lt;/span&gt;
&lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;ChildItem&lt;/span&gt; &lt;span class="nv"&gt;$dir&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Where&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="nc"&gt;Name&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;match&lt;/span&gt; &lt;span class="s1"&gt;'\.log$'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Select&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;ExpandProperty&lt;/span&gt; &lt;span class="nc"&gt;Name&lt;/span&gt;

&lt;span class="nc"&gt;Remove&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Item&lt;/span&gt; &lt;span class="nv"&gt;$dir&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Recurse&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Force&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.log
backup.log.zip
catalog.txt
error-log.txt
---
app.log
app.log
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both fixes return only &lt;code&gt;app.log&lt;/code&gt;. For simple file name filters, &lt;code&gt;-like&lt;/code&gt; is usually the clearer choice. Reach for &lt;code&gt;-match&lt;/code&gt; when you actually need regex features like &lt;code&gt;\d+&lt;/code&gt;, alternatives (&lt;code&gt;error|warn&lt;/code&gt;), or capture groups.&lt;/p&gt;

&lt;p&gt;If you want to try these patterns without setting up anything, &lt;a href="https://windows-cli.arnost.org/en" rel="noopener noreferrer"&gt;CMD Master's free interactive PowerShell course in the browser&lt;/a&gt; gives you a real terminal to experiment in.&lt;/p&gt;

&lt;h2&gt;
  
  
  Quick checklist
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Matching file names like &lt;code&gt;*.log&lt;/code&gt; or &lt;code&gt;report-2026-*.csv&lt;/code&gt;: use &lt;code&gt;-like&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Need digits, alternatives, or pieces of the text: use &lt;code&gt;-match&lt;/code&gt; and read &lt;code&gt;$Matches&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Using &lt;code&gt;-match&lt;/code&gt; for a whole value: anchor it with &lt;code&gt;^&lt;/code&gt; and &lt;code&gt;$&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Remember the regex &lt;code&gt;.&lt;/code&gt; is any character. Write &lt;code&gt;\.&lt;/code&gt; for a real dot.&lt;/li&gt;
&lt;li&gt;Running &lt;code&gt;-match&lt;/code&gt; on an array: you get filtered items, not a fresh &lt;code&gt;$Matches&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Need case to matter: &lt;code&gt;-clike&lt;/code&gt; / &lt;code&gt;-cmatch&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Pattern comes from a variable: &lt;code&gt;[WildcardPattern]::Escape()&lt;/code&gt; for &lt;code&gt;-like&lt;/code&gt;, &lt;code&gt;[regex]::Escape()&lt;/code&gt; for &lt;code&gt;-match&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Practice in a real terminal
&lt;/h2&gt;

&lt;p&gt;Wildcards and regex are much easier to learn by typing them than by reading tables. &lt;a href="https://windows-cli.arnost.org/en" rel="noopener noreferrer"&gt;CMD Master&lt;/a&gt; is a free, interactive browser terminal for learning CMD, PowerShell, and Bash, with instant feedback on every command and nothing to install.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sources:&lt;/strong&gt; Microsoft Learn: &lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_comparison_operators" rel="noopener noreferrer"&gt;about_Comparison_Operators&lt;/a&gt;, &lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_wildcards" rel="noopener noreferrer"&gt;about_Wildcards&lt;/a&gt;, &lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_regular_expressions" rel="noopener noreferrer"&gt;about_Regular_Expressions&lt;/a&gt;, &lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables#matches" rel="noopener noreferrer"&gt;about_Automatic_Variables ($Matches)&lt;/a&gt;, &lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/where-object" rel="noopener noreferrer"&gt;Where-Object&lt;/a&gt;, &lt;a href="https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.wildcardpattern.escape" rel="noopener noreferrer"&gt;WildcardPattern.Escape&lt;/a&gt;, &lt;a href="https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.escape" rel="noopener noreferrer"&gt;Regex.Escape&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Quick quiz
&lt;/h2&gt;

&lt;p&gt;Without running it: what does this line output in PowerShell 7.4?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="s1"&gt;'app.log'&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;match&lt;/span&gt; &lt;span class="s1"&gt;'*.log'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Is it &lt;code&gt;True&lt;/code&gt;, &lt;code&gt;False&lt;/code&gt;, or something else? Drop your answer (and why) in the comments.&lt;/p&gt;

</description>
      <category>powershell</category>
      <category>scripting</category>
      <category>windows</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Windows RMDIR Command: Delete a Folder and Everything Inside It</title>
      <dc:creator>arnostorg</dc:creator>
      <pubDate>Wed, 11 Nov 2026 08:32:57 +0000</pubDate>
      <link>https://dev.to/arnostorg/windows-rmdir-command-delete-a-folder-and-everything-inside-it-339b</link>
      <guid>https://dev.to/arnostorg/windows-rmdir-command-delete-a-folder-and-everything-inside-it-339b</guid>
      <description>&lt;p&gt;You try to delete an old build folder from CMD and get this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;C:\&amp;gt;rmdir C:\build\old
The directory is not empty.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;rmdir&lt;/code&gt; is careful by default. It only removes &lt;strong&gt;empty&lt;/strong&gt; folders. To delete a folder together with every file and subfolder inside it, you need two switches, and you should know exactly what they do before you type them, because CMD has no Recycle Bin and no undo.&lt;/p&gt;

&lt;p&gt;This post covers the version I actually use: empty vs. full folders, &lt;code&gt;/s&lt;/code&gt; and &lt;code&gt;/q&lt;/code&gt;, paths with spaces, clearing a folder but keeping it, and the batch-script guard that stops &lt;code&gt;rd&lt;/code&gt; from going after the wrong drive.&lt;/p&gt;

&lt;h2&gt;
  
  
  RD and RMDIR are the same command
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;rd&lt;/code&gt; is just the short name. Microsoft documents them as the same command, so everything below works with either spelling:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rmdir C:\build\old
rd C:\build\old
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Syntax, straight from the docs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rmdir [&amp;lt;drive&amp;gt;:]&amp;lt;path&amp;gt; [/s [/q]]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Delete an empty folder
&lt;/h2&gt;

&lt;p&gt;With no switches, &lt;code&gt;rd&lt;/code&gt; deletes the folder only if it is empty:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rd C:\build\empty-folder
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's the safe default. If anything is inside, including &lt;strong&gt;hidden or system files&lt;/strong&gt; you can't see in a normal &lt;code&gt;dir&lt;/code&gt;, you get &lt;code&gt;The directory is not empty.&lt;/code&gt; and nothing is deleted.&lt;/p&gt;

&lt;p&gt;To see what's blocking it, list everything, hidden files included:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dir /a C:\build\old
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Delete a folder and everything inside it: /s and /q
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rd /s C:\build\old
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;/s&lt;/code&gt; deletes the &lt;strong&gt;whole directory tree&lt;/strong&gt;: the folder, all its subfolders, and all files.&lt;/li&gt;
&lt;li&gt;Without &lt;code&gt;/q&lt;/code&gt;, &lt;code&gt;rd /s&lt;/code&gt; &lt;strong&gt;asks for confirmation&lt;/strong&gt; before deleting the tree. Answer &lt;code&gt;Y&lt;/code&gt; to go ahead, &lt;code&gt;N&lt;/code&gt; to cancel.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For scripts, where nobody is there to answer, add &lt;code&gt;/q&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rd /s /q C:\build\old
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;/q&lt;/code&gt; is quiet mode: &lt;strong&gt;no confirmation prompt&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;/q&lt;/code&gt; only works together with &lt;code&gt;/s&lt;/code&gt;. On its own it does nothing useful.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Microsoft's own docs put a caution box on this one: in quiet mode the entire tree is deleted without confirmation. Treat &lt;code&gt;rd /s /q&lt;/code&gt; like a loaded command and always know what the path resolves to.&lt;/p&gt;

&lt;p&gt;Want to try the folder commands without risking a real disk? You can &lt;a href="https://windows-cli.arnost.org/en" rel="noopener noreferrer"&gt;practice CMD folder commands like rd, mkdir and dir in a free browser terminal&lt;/a&gt; and break things on purpose.&lt;/p&gt;

&lt;h2&gt;
  
  
  Preview before you delete
&lt;/h2&gt;

&lt;p&gt;Before running &lt;code&gt;rd /s /q&lt;/code&gt; on anything that matters, look at what's in there:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;:: every file and folder, full paths, one per line
dir /s /b C:\build\old

:: the same, drawn as a tree with the files
tree /f C:\build\old
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If that list is longer than you expected, stop and check the path. Ten seconds of reading beats restoring from backup.&lt;/p&gt;

&lt;h2&gt;
  
  
  Paths with spaces need quotes
&lt;/h2&gt;

&lt;p&gt;CMD splits arguments on spaces. Without quotes, this does not mean what it looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rd /s /q C:\Temp\Old Builds
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;rd&lt;/code&gt; sees &lt;strong&gt;two&lt;/strong&gt; paths: &lt;code&gt;C:\Temp\Old&lt;/code&gt; and &lt;code&gt;Builds&lt;/code&gt; (relative to the current folder). Best case, you get errors. Worst case, a folder called &lt;code&gt;Builds&lt;/code&gt; in your current directory really exists and is gone.&lt;/p&gt;

&lt;p&gt;Always quote paths:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rd /s /q "C:\Temp\Old Builds"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Delete the contents but keep the folder
&lt;/h2&gt;

&lt;p&gt;Sometimes the folder itself has to stay (a share, a path an app expects, a folder with custom permissions) and only the stuff inside should go.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Option 1: delete and recreate.&lt;/strong&gt; Simple and fine for scratch folders:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rd /s /q "C:\build\output"
if not exist "C:\build\output" mkdir "C:\build\output"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Keep in mind the recreated folder is a &lt;em&gt;new&lt;/em&gt; folder: custom permissions or attributes set on the old one are not carried over.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Option 2: delete only what's inside.&lt;/strong&gt; Files first, then each subfolder:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;del /q "C:\build\output\*"
for /d %i in ("C:\build\output\*") do rd /s /q "%i"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;del /q&lt;/code&gt; removes the files directly inside the folder without prompting.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;for /d&lt;/code&gt; loops over the subfolders, and &lt;code&gt;rd /s /q&lt;/code&gt; removes each one with everything in it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In a &lt;code&gt;.bat&lt;/code&gt; or &lt;code&gt;.cmd&lt;/code&gt; file you must double the percent signs on the loop variable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight batchfile"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;%%&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"C:\build\output\*"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="nb"&gt;rd&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;q&lt;/span&gt; &lt;span class="s2"&gt;"%%i"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At the prompt it's &lt;code&gt;%i&lt;/code&gt;, in a batch file it's &lt;code&gt;%%i&lt;/code&gt;. Mixing those up is the most common reason this line "does nothing".&lt;/p&gt;

&lt;p&gt;Hidden files and folders need extra care here; check with &lt;code&gt;dir /a&lt;/code&gt; afterwards and see the &lt;code&gt;del&lt;/code&gt; and &lt;code&gt;attrib&lt;/code&gt; docs if something survived.&lt;/p&gt;

&lt;h2&gt;
  
  
  The batch-script trap: an empty variable
&lt;/h2&gt;

&lt;p&gt;This is the one that actually causes outages. A cleanup script like this looks harmless:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight batchfile"&gt;&lt;code&gt;&lt;span class="nb"&gt;rd&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;q&lt;/span&gt; &lt;span class="s2"&gt;"%TARGET%\"
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now imagine &lt;code&gt;TARGET&lt;/code&gt; was never set, because of a typo, a missing argument, or a different user context. The line becomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight batchfile"&gt;&lt;code&gt;&lt;span class="nb"&gt;rd&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;q&lt;/span&gt; &lt;span class="s2"&gt;"\"
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A path that starts with a backslash is resolved from the &lt;strong&gt;root of the current drive&lt;/strong&gt;. So instead of deleting one folder, &lt;code&gt;rd&lt;/code&gt; is now pointed at the top of the drive. Best case, you get a wall of access-denied errors. Worst case, it quietly deletes everything the account has permission to delete.&lt;/p&gt;

&lt;p&gt;Guard every &lt;code&gt;rd /s /q&lt;/code&gt; that uses a variable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight batchfile"&gt;&lt;code&gt;&lt;span class="nd"&gt;@echo&lt;/span&gt; &lt;span class="n"&gt;off&lt;/span&gt;
&lt;span class="k"&gt;setlocal&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;not&lt;/span&gt; &lt;span class="n"&gt;defined&lt;/span&gt; &lt;span class="no"&gt;TARGET&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="no"&gt;TARGET&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="k"&gt;not&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="nc"&gt;Refusing&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;delete&lt;/span&gt; &lt;span class="n"&gt;anything&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
  &lt;span class="k"&gt;exit&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;not&lt;/span&gt; &lt;span class="k"&gt;exist&lt;/span&gt; &lt;span class="s2"&gt;"%TARGET%\" (
  echo "&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="no"&gt;TARGET&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="s2"&gt;" does not exist. Nothing to delete.
  exit /b 0
)

rd /s /q "&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="no"&gt;TARGET&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="s2"&gt;"

if exist "&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="no"&gt;TARGET&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;\&lt;span class="s2"&gt;" (
  echo Some items in "&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="no"&gt;TARGET&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="s2"&gt;" could not be deleted.
  exit /b 1
)

echo Deleted "&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="no"&gt;TARGET&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="s2"&gt;".
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What each part does:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;if not defined TARGET&lt;/code&gt; stops the script when the variable is missing.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;if not exist "%TARGET%\"&lt;/code&gt; (with the trailing backslash) checks that the path exists &lt;em&gt;as a folder&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;The final &lt;code&gt;if exist&lt;/code&gt; checks the result. Don't rely on &lt;code&gt;%errorlevel%&lt;/code&gt; alone after &lt;code&gt;rd&lt;/code&gt;; checking whether the folder is still there is the reliable test.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The same rule applies to &lt;code&gt;%1&lt;/code&gt; and other script arguments: never feed them straight into &lt;code&gt;rd /s /q&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  When rd can't delete everything
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;"Access is denied" or "being used by another process."&lt;/strong&gt; A file is open in some program (an editor, a running service, an antivirus scan) or your account lacks permission. &lt;code&gt;rd /s /q&lt;/code&gt; deletes what it can and leaves the rest. Close the program or stop the service, then run it again. For permission problems, run CMD as administrator only if you are sure about the path.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;You're standing in the folder.&lt;/strong&gt; You can't delete the current directory. Microsoft's docs show the error you get as &lt;em&gt;"The process can't access the file because it is being used by another process."&lt;/em&gt; Move out first:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd /d C:\
rd /s /q "C:\build\old"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note the parent folder isn't enough if your prompt is inside a &lt;em&gt;subfolder&lt;/em&gt; of what you're deleting. Change to a folder outside the tree.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Very long paths.&lt;/strong&gt; Classic Windows APIs limit a path to &lt;code&gt;MAX_PATH&lt;/code&gt;, 260 characters. Deeply nested folders (looking at you, &lt;code&gt;node_modules&lt;/code&gt;) can end up longer than that, and &lt;code&gt;rd&lt;/code&gt; then fails on them. A widely used workaround is to mirror an &lt;strong&gt;empty&lt;/strong&gt; folder over the problem folder with Robocopy, which empties it, then remove the leftovers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir C:\empty
robocopy C:\empty "C:\build\deep-folder" /mir
rd /s /q "C:\build\deep-folder"
rd C:\empty
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;/mir&lt;/code&gt; deletes everything in the destination that isn't in the source, and the source is empty, so be very sure about the destination path. I covered &lt;code&gt;/mir&lt;/code&gt; and its risks in &lt;a href="https://dev.to/arnostorg/windows-robocopy-command-mirror-a-folder-without-deleting-the-wrong-files-dm0"&gt;Windows Robocopy Command: Mirror a Folder Without Deleting the Wrong Files&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The PowerShell equivalent (with a real dry run)
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;rd&lt;/code&gt; has no preview mode. PowerShell's &lt;code&gt;Remove-Item&lt;/code&gt; does:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="nc"&gt;Remove&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Item&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="s1"&gt;'C:\build\old'&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Recurse&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Force&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;WhatIf&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;-WhatIf&lt;/code&gt; shows what would be deleted without deleting anything. Drop it when the list looks right. I wrote a full post on this: &lt;a href="https://dev.to/arnostorg/powershell-remove-item-dry-run-deletes-with-whatif-before-you-nuke-folders-14ec"&gt;PowerShell Remove-Item: Dry-Run Deletes with -WhatIf Before You Nuke Folders&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cheat sheet
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Goal&lt;/th&gt;
&lt;th&gt;Command&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Delete an empty folder&lt;/td&gt;
&lt;td&gt;&lt;code&gt;rd "C:\path"&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Delete a folder and everything inside (asks first)&lt;/td&gt;
&lt;td&gt;&lt;code&gt;rd /s "C:\path"&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Same, no prompt (scripts)&lt;/td&gt;
&lt;td&gt;&lt;code&gt;rd /s /q "C:\path"&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Preview what's inside&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;dir /s /b "C:\path"&lt;/code&gt; or &lt;code&gt;tree /f "C:\path"&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;See hidden files blocking delete&lt;/td&gt;
&lt;td&gt;&lt;code&gt;dir /a "C:\path"&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Empty a folder, keep it&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;del /q "C:\path\*"&lt;/code&gt; + &lt;code&gt;for /d %i in ("C:\path\*") do rd /s /q "%i"&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Guard a variable in a .bat&lt;/td&gt;
&lt;td&gt;&lt;code&gt;if not defined TARGET exit /b 1&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Dry run in PowerShell&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Remove-Item 'C:\path' -Recurse -Force -WhatIf&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Related CMD basics: &lt;a href="https://dev.to/arnostorg/windows-mkdir-command-create-folders-fast-from-cmd-47c3"&gt;Windows MKDIR Command: Create Folders Fast From CMD&lt;/a&gt; and &lt;a href="https://dev.to/arnostorg/windows-del-command-delete-files-safely-from-cmd-2nea"&gt;Windows DEL Command: Delete Files Safely From CMD&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Official references on Microsoft Learn: &lt;a href="https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/rmdir" rel="noopener noreferrer"&gt;rmdir&lt;/a&gt;, &lt;a href="https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/del" rel="noopener noreferrer"&gt;del&lt;/a&gt;, &lt;a href="https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/for" rel="noopener noreferrer"&gt;for&lt;/a&gt;, &lt;a href="https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/if" rel="noopener noreferrer"&gt;if&lt;/a&gt;, &lt;a href="https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/dir" rel="noopener noreferrer"&gt;dir&lt;/a&gt;, &lt;a href="https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/tree" rel="noopener noreferrer"&gt;tree&lt;/a&gt;, &lt;a href="https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/attrib" rel="noopener noreferrer"&gt;attrib&lt;/a&gt;, &lt;a href="https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy" rel="noopener noreferrer"&gt;robocopy&lt;/a&gt;, &lt;a href="https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation" rel="noopener noreferrer"&gt;Maximum path length limitation&lt;/a&gt;, and &lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/remove-item" rel="noopener noreferrer"&gt;Remove-Item&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Practice it where mistakes are free
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;rd /s /q&lt;/code&gt; is a command you want in muscle memory, but not one you want to learn on a production server. &lt;a href="https://windows-cli.arnost.org/en" rel="noopener noreferrer"&gt;CMD Master's free interactive Windows command line course&lt;/a&gt; gives you a real terminal in the browser with hands-on CMD, PowerShell, and Bash drills and instant feedback, so you can create, list, and delete folder trees as often as you like.&lt;/p&gt;

&lt;p&gt;Quick quiz to finish: in a batch file, what does &lt;code&gt;cd /d %WORK% &amp;amp; rd /s /q .&lt;/code&gt; do if &lt;code&gt;%WORK%&lt;/code&gt; was never set? Write your answer in the comments before you test it.&lt;/p&gt;

</description>
      <category>windows</category>
      <category>commandline</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>PowerShell ForEach-Object vs foreach: The Real Difference (and Why return Doesn't Stop Your Loop)</title>
      <dc:creator>arnostorg</dc:creator>
      <pubDate>Mon, 09 Nov 2026 08:25:31 +0000</pubDate>
      <link>https://dev.to/arnostorg/powershell-foreach-object-vs-foreach-the-real-difference-and-why-return-doesnt-stop-your-loop-1599</link>
      <guid>https://dev.to/arnostorg/powershell-foreach-object-vs-foreach-the-real-difference-and-why-return-doesnt-stop-your-loop-1599</guid>
      <description>&lt;p&gt;You wrote &lt;code&gt;return&lt;/code&gt; inside a loop to stop at the first match. The loop kept going. Or you wrote &lt;code&gt;break&lt;/code&gt; to skip the rest of one inner loop, and your whole outer loop quietly ended.&lt;/p&gt;

&lt;p&gt;Both bugs come from the same mix-up: &lt;code&gt;ForEach-Object&lt;/code&gt; and the &lt;code&gt;foreach&lt;/code&gt; statement look alike, but they are different things. One is a cmdlet that runs a script block per pipeline item. The other is a language loop. &lt;code&gt;return&lt;/code&gt;, &lt;code&gt;break&lt;/code&gt;, and &lt;code&gt;continue&lt;/code&gt; behave differently in each.&lt;/p&gt;

&lt;p&gt;Everything below was run in PowerShell 7.4.6. The same rules apply in Windows PowerShell 5.1.&lt;/p&gt;

&lt;h2&gt;
  
  
  The shortest honest comparison
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;
&lt;code&gt;ForEach-Object&lt;/code&gt; (cmdlet)&lt;/th&gt;
&lt;th&gt;
&lt;code&gt;foreach (...) { }&lt;/code&gt; (statement)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Input&lt;/td&gt;
&lt;td&gt;Pipeline, one item at a time&lt;/td&gt;
&lt;td&gt;A collection already in memory&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Current item&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;$_&lt;/code&gt; / &lt;code&gt;$PSItem&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Your own variable (&lt;code&gt;$file&lt;/code&gt;, &lt;code&gt;$n&lt;/code&gt;)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;return&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Skips to the &lt;strong&gt;next item&lt;/strong&gt; (like &lt;code&gt;continue&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;Leaves the &lt;strong&gt;function or script&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;break&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Looks for an enclosing loop &lt;strong&gt;outside&lt;/strong&gt; the pipeline&lt;/td&gt;
&lt;td&gt;Exits this loop&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Pipe the result onward&lt;/td&gt;
&lt;td&gt;Works directly&lt;/td&gt;
&lt;td&gt;Needs &lt;code&gt;$( )&lt;/code&gt; or a variable first&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Speed on big in-memory lists&lt;/td&gt;
&lt;td&gt;Slower (per-item script block call)&lt;/td&gt;
&lt;td&gt;Faster&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;One extra trap: when &lt;code&gt;foreach&lt;/code&gt; appears &lt;strong&gt;after a pipe&lt;/strong&gt;, it is not the statement. It is an alias:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Get-Alias&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;foreach&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Definition&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="c"&gt;# ForEach-Object&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So &lt;code&gt;Get-ChildItem | foreach { ... }&lt;/code&gt; is &lt;code&gt;ForEach-Object&lt;/code&gt;, with all of its rules. Microsoft Learn's &lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_foreach" rel="noopener noreferrer"&gt;about_Foreach&lt;/a&gt; covers the statement, and &lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/foreach-object" rel="noopener noreferrer"&gt;ForEach-Object&lt;/a&gt; covers the cmdlet.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why &lt;code&gt;return&lt;/code&gt; doesn't stop your loop
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ForEach-Object&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="kr"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;$_&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;-eq&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kr"&gt;return&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"item &lt;/span&gt;&lt;span class="bp"&gt;$_&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;item 1
item 2
item 4
item 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;return&lt;/code&gt; only left the script block for item 3. &lt;code&gt;ForEach-Object&lt;/code&gt; then called the same script block for item 4. In practice &lt;code&gt;return&lt;/code&gt; inside &lt;code&gt;ForEach-Object&lt;/code&gt; acts like &lt;code&gt;continue&lt;/code&gt; in a normal loop (&lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_return" rel="noopener noreferrer"&gt;about_Return&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;foreach&lt;/code&gt; statement does what most people expected:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="kr"&gt;function&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;Test-Foreach&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="kr"&gt;foreach&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$n&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kr"&gt;in&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="kr"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$n&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;-eq&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kr"&gt;return&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"item &lt;/span&gt;&lt;span class="nv"&gt;$n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s1"&gt;'after loop'&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="n"&gt;Test-Foreach&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;item 1
item 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here &lt;code&gt;return&lt;/code&gt; left the whole function, so even &lt;code&gt;'after loop'&lt;/code&gt; never ran. If you only want to skip one item, use &lt;code&gt;continue&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="kr"&gt;foreach&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$n&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kr"&gt;in&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="kr"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$n&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;-eq&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kr"&gt;continue&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"item &lt;/span&gt;&lt;span class="nv"&gt;$n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="c"&gt;# item 1, item 2, item 4, item 5&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Why &lt;code&gt;break&lt;/code&gt; kills your outer loop
&lt;/h2&gt;

&lt;p&gt;This one is nastier, because nothing errors:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="kr"&gt;foreach&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$outer&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kr"&gt;in&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'A'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'B'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ForEach-Object&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="kr"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;$_&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;-eq&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kr"&gt;break&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$outer&lt;/span&gt;&lt;span class="bp"&gt;$_&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="s1"&gt;'after outer'&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A1
after outer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;B&lt;/code&gt; never ran. The &lt;code&gt;ForEach-Object&lt;/code&gt; script block is not a loop, so &lt;code&gt;break&lt;/code&gt; went looking for the nearest enclosing loop and found the outer &lt;code&gt;foreach&lt;/code&gt;. Microsoft Learn's &lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_break" rel="noopener noreferrer"&gt;about_Break&lt;/a&gt; warns about this directly: &lt;code&gt;break&lt;/code&gt; inside a pipeline such as a &lt;code&gt;ForEach-Object&lt;/code&gt; script block can exit far more than the pipeline. With no enclosing loop at all, it can stop the whole script.&lt;/p&gt;

&lt;p&gt;If you need to stop early, pick a tool built for it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Stop the pipeline after the first N results&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;1000000&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ForEach-Object&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="bp"&gt;$_&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Select-Object&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-First&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;3&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="c"&gt;# 1, 2, 3&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="c"&gt;# Or use statements and a labeled break&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;outer&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;foreach&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$a&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kr"&gt;in&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="kr"&gt;foreach&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$b&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kr"&gt;in&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="kr"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$b&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;-eq&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kr"&gt;break&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;outer&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$a&lt;/span&gt;&lt;span class="s2"&gt;-&lt;/span&gt;&lt;span class="nv"&gt;$b&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="c"&gt;# 1-1&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Select-Object -First&lt;/code&gt; stops the upstream pipeline once it has enough items. Labels are explained in &lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_break" rel="noopener noreferrer"&gt;about_Break&lt;/a&gt; and &lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_continue" rel="noopener noreferrer"&gt;about_Continue&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Piping and speed
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;foreach&lt;/code&gt; statement can't sit at the start of a pipeline:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="kr"&gt;foreach&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$n&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kr"&gt;in&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;$n&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Sort-Object&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Descending&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="c"&gt;# An empty pipe element is not allowed.&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Wrap it in &lt;code&gt;$( )&lt;/code&gt;, or assign it first:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kr"&gt;foreach&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$n&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kr"&gt;in&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;$n&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Sort-Object&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Descending&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="c"&gt;# 3, 2, 1&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="nv"&gt;$doubled&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kr"&gt;foreach&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$n&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kr"&gt;in&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;$n&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="nv"&gt;$doubled&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;-join&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;','&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="c"&gt;# 2,4,6&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On speed, I summed 200,000 numbers both ways on the same box, three runs each. The &lt;code&gt;foreach&lt;/code&gt; statement took about 150 to 300 ms. &lt;code&gt;ForEach-Object&lt;/code&gt; took about 740 to 900 ms. Your numbers will differ, but the gap is normal: the cmdlet invokes a script block for every item.&lt;/p&gt;

&lt;p&gt;That doesn't make &lt;code&gt;ForEach-Object&lt;/code&gt; the wrong choice. It streams, so it starts working on the first file while &lt;code&gt;Get-ChildItem&lt;/code&gt; is still finding the rest, and it never needs the whole list in memory. It also has &lt;code&gt;-Begin&lt;/code&gt; and &lt;code&gt;-End&lt;/code&gt; blocks for setup and totals:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ForEach-Object&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Begin&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;$t&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Process&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;$t&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;+=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="bp"&gt;$_&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-End&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"total &lt;/span&gt;&lt;span class="nv"&gt;$t&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="c"&gt;# total 10&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Quick checklist
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Data already in a variable, and you want speed or real &lt;code&gt;break&lt;/code&gt;/&lt;code&gt;return&lt;/code&gt;: use the &lt;code&gt;foreach&lt;/code&gt; statement.&lt;/li&gt;
&lt;li&gt;Streaming a big pipeline (files, log lines, remote results): use &lt;code&gt;ForEach-Object&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Want to skip one item inside &lt;code&gt;ForEach-Object&lt;/code&gt;: &lt;code&gt;return&lt;/code&gt; works, and that's the only thing it does there.&lt;/li&gt;
&lt;li&gt;Want to stop a pipeline early: &lt;code&gt;Select-Object -First&lt;/code&gt;, not &lt;code&gt;break&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Seeing &lt;code&gt;| foreach {&lt;/code&gt; in a script: that's &lt;code&gt;ForEach-Object&lt;/code&gt;, not the statement.&lt;/li&gt;
&lt;li&gt;Need to pipe a &lt;code&gt;foreach&lt;/code&gt; statement's output: wrap it in &lt;code&gt;$( )&lt;/code&gt; or assign it first.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Practice loops in a real terminal
&lt;/h2&gt;

&lt;p&gt;Reading about loop control is quick; spotting it inside someone else's script is the hard part. &lt;a href="https://windows-cli.arnost.org/en" rel="noopener noreferrer"&gt;CMD Master's free interactive PowerShell, CMD, and Bash terminal in the browser&lt;/a&gt; lets you run these loops with no install and see exactly where &lt;code&gt;return&lt;/code&gt; and &lt;code&gt;break&lt;/code&gt; land.&lt;/p&gt;

&lt;p&gt;Have you ever had a &lt;code&gt;break&lt;/code&gt; inside &lt;code&gt;ForEach-Object&lt;/code&gt; end more of your script than you meant it to?&lt;/p&gt;

</description>
      <category>powershell</category>
      <category>scripting</category>
      <category>windows</category>
      <category>beginners</category>
    </item>
    <item>
      <title>PowerShell Write-Host vs Write-Output: The Real Difference (and Why Your Logs Are Empty)</title>
      <dc:creator>arnostorg</dc:creator>
      <pubDate>Fri, 06 Nov 2026 08:30:07 +0000</pubDate>
      <link>https://dev.to/arnostorg/powershell-write-host-vs-write-output-the-real-difference-and-why-your-logs-are-empty-545i</link>
      <guid>https://dev.to/arnostorg/powershell-write-host-vs-write-output-the-real-difference-and-why-your-logs-are-empty-545i</guid>
      <description>&lt;p&gt;You redirected a script to a log file, opened the log, and got… nothing. The console showed every status line while the script ran. The file was empty. Or you called a helper function, assigned the result, and &lt;code&gt;$result&lt;/code&gt; was &lt;code&gt;$null&lt;/code&gt; even though the function clearly "printed" a value.&lt;/p&gt;

&lt;p&gt;That is not a broken redirect. That is &lt;code&gt;Write-Host&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Write-Host&lt;/code&gt; writes for the person watching the console. &lt;code&gt;Write-Output&lt;/code&gt; (and bare values) write objects into the success stream — the pipeline everyone else can capture, redirect, and log. Mix them up and your logs go quiet.&lt;/p&gt;

&lt;p&gt;Everything below was run in PowerShell 7.4.6. The same stream rules apply in Windows PowerShell 5.1.&lt;/p&gt;

&lt;h2&gt;
  
  
  The shortest honest comparison
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;Write-Host&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;
&lt;code&gt;Write-Output&lt;/code&gt; / bare value&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Destination&lt;/td&gt;
&lt;td&gt;Host UI (information stream)&lt;/td&gt;
&lt;td&gt;Success stream / pipeline&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;$x = …&lt;/code&gt; captures it?&lt;/td&gt;
&lt;td&gt;No — &lt;code&gt;$x&lt;/code&gt; is &lt;code&gt;$null&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;&amp;gt; log.txt&lt;/code&gt; / &lt;code&gt;Out-File&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Empty file (host still prints)&lt;/td&gt;
&lt;td&gt;File has the text&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Best for&lt;/td&gt;
&lt;td&gt;Progress, color, "still running…"&lt;/td&gt;
&lt;td&gt;Data a caller or log must keep&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Microsoft Learn's &lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/write-host" rel="noopener noreferrer"&gt;Write-Host&lt;/a&gt; page says it plainly: primary purpose is host-display-only output. &lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/write-output" rel="noopener noreferrer"&gt;Write-Output&lt;/a&gt; sends objects to the success stream. &lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_output_streams" rel="noopener noreferrer"&gt;about_Output_Streams&lt;/a&gt; maps stream 1 to &lt;code&gt;Write-Output&lt;/code&gt; and stream 6 to &lt;code&gt;Write-Information&lt;/code&gt; / &lt;code&gt;Write-Host&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The empty-log bug
&lt;/h2&gt;

&lt;p&gt;Here is the failure that starts most of these tickets:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;Write-Host&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'Starting job...'&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;Write-Host&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'Done.'&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;/job.log&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Get-Item&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;/job.log&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Length&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="c"&gt;# 0&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="n"&gt;Get-Content&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;/job.log&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Raw&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="c"&gt;# (empty)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The console still printed both lines. The file is zero bytes. Plain &lt;code&gt;&amp;gt;&lt;/code&gt; only redirects the success stream (see &lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_redirection" rel="noopener noreferrer"&gt;about_Redirection&lt;/a&gt;). &lt;code&gt;Write-Host&lt;/code&gt; never put anything there.&lt;/p&gt;

&lt;p&gt;Swap in &lt;code&gt;Write-Output&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;Write-Output&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'Starting job...'&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;Write-Output&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'Done.'&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;/job2.log&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="n"&gt;Get-Content&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;/job2.log&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Starting job...
Done.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Same shape with &lt;code&gt;Out-File&lt;/code&gt; and the pipeline:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;Write-Host&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'hi-host-of'&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Out-File&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;/log-host-of.txt&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Get-Item&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;/log-host-of.txt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Length&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="c"&gt;# 0&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="n"&gt;Write-Output&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'hi-out-of'&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Out-File&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;/log-out-of.txt&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;Get-Content&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;/log-out-of.txt&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="c"&gt;# hi-out-of&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Assignment and the pipeline
&lt;/h2&gt;

&lt;p&gt;Capture makes the difference impossible to miss:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$x&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Write-Host&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'hi-host'&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="bp"&gt;$null&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;-eq&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;$x&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="c"&gt;# True&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="nv"&gt;$y&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Write-Output&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'hi-out'&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="nv"&gt;$y&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="c"&gt;# hi-out&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="nv"&gt;$z&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'hi-bare'&lt;/span&gt;&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="c"&gt;# bare value = same success stream as Write-Output&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="nv"&gt;$z&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="c"&gt;# hi-bare&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pipeline consumers see the same split:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Write-Host&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'hi'&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Measure-Object&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Count&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="c"&gt;# 0&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Write-Output&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'hi'&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Measure-Object&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Count&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="c"&gt;# 1&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Write-Host&lt;/code&gt; painted &lt;code&gt;hi&lt;/code&gt; on the screen. The next cmdlet received nothing on the success stream.&lt;/p&gt;

&lt;h2&gt;
  
  
  Functions that "print" return nothing useful
&lt;/h2&gt;

&lt;p&gt;This is the quiet version of the empty-log bug — it shows up inside modules and helpers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="kr"&gt;function&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;Show-Bad&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;Write-Host&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'Computing...'&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;Write-Host&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;42&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="kr"&gt;function&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;Get-Good&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;Write-Host&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'Computing status...'&lt;/span&gt;&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="c"&gt;# fine: human status&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;Write-Output&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;42&lt;/span&gt;&lt;span class="w"&gt;                    &lt;/span&gt;&lt;span class="c"&gt;# data for the caller&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="kr"&gt;function&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;Get-Best&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="w"&gt;                                 &lt;/span&gt;&lt;span class="c"&gt;# bare value is enough&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="nv"&gt;$a&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Show-Bad&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="bp"&gt;$null&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;-eq&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;$a&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="c"&gt;# True&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="nv"&gt;$b&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Get-Good&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="nv"&gt;$b&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="c"&gt;# 42&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="nv"&gt;$c&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Get-Best&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="nv"&gt;$c&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="c"&gt;# 42&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Show-Bad&lt;/code&gt; looked busy. The caller got &lt;code&gt;$null&lt;/code&gt;. PSScriptAnalyzer's &lt;a href="https://learn.microsoft.com/en-us/powershell/utility-modules/psscriptanalyzer/rules/avoidusingwritehost" rel="noopener noreferrer"&gt;AvoidUsingWriteHost&lt;/a&gt; rule exists for exactly this: unless the verb is &lt;code&gt;Show&lt;/code&gt;, prefer &lt;code&gt;Write-Output&lt;/code&gt; / implicit output for data and &lt;code&gt;Write-Verbose&lt;/code&gt; for optional chatter.&lt;/p&gt;

&lt;h2&gt;
  
  
  When Write-Host is the right tool
&lt;/h2&gt;

&lt;p&gt;Keep &lt;code&gt;Write-Host&lt;/code&gt; when a human watching the console is the consumer — colored prompts, step banners, "still scanning…" lines you do &lt;strong&gt;not&lt;/strong&gt; want in the data pipeline:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;Write-Host&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'Scanning C:\Logs...'&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-ForegroundColor&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Cyan&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="nv"&gt;$files&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Get-ChildItem&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;C:\Logs&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-File&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;Write-Host&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Found &lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nv"&gt;$files&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Count&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s2"&gt; files."&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-ForegroundColor&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Green&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="nv"&gt;$files&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Select-Object&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Length&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Export-Csv&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;\report.csv&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-NoTypeInformation&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Status stayed on screen. The CSV only got file objects. That split is intentional.&lt;/p&gt;

&lt;h2&gt;
  
  
  Write-Information, stream 6, and a 7.x note
&lt;/h2&gt;

&lt;p&gt;Since Windows PowerShell 5.0, &lt;code&gt;Write-Host&lt;/code&gt; is a wrapper around &lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/write-information" rel="noopener noreferrer"&gt;Write-Information&lt;/a&gt; and lands on the information stream (stream 6). That is why you &lt;em&gt;can&lt;/em&gt; capture it if you ask for stream 6 on purpose:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;Write-Host&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'hi-info'&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;6&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;/log-info.txt&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;Get-Content&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;/log-info.txt&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="c"&gt;# hi-info&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two gotchas that trip people on 7.x:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Default &lt;code&gt;$InformationPreference&lt;/code&gt; is &lt;code&gt;SilentlyContinue&lt;/code&gt;. &lt;code&gt;Write-Information&lt;/code&gt; stays quiet unless you set the preference or pass &lt;code&gt;-InformationAction Continue&lt;/code&gt;. &lt;code&gt;Write-Host&lt;/code&gt; still displays anyway — Microsoft Learn notes that &lt;code&gt;$InformationPreference&lt;/code&gt; does &lt;strong&gt;not&lt;/strong&gt; suppress &lt;code&gt;Write-Host&lt;/code&gt;, except &lt;code&gt;-InformationAction Ignore&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Prefer &lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/write-verbose" rel="noopener noreferrer"&gt;Write-Verbose&lt;/a&gt; for optional diagnostics the user can turn on with &lt;code&gt;-Verbose&lt;/code&gt;. Prefer bare values / &lt;code&gt;Write-Output&lt;/code&gt; for anything a caller must keep.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Quick checklist
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Building data a function, pipeline, log, or file must keep: bare value or &lt;code&gt;Write-Output&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Banner / progress / colored console-only status: &lt;code&gt;Write-Host&lt;/code&gt; (or &lt;code&gt;Write-Information&lt;/code&gt; / &lt;code&gt;Write-Verbose&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Empty log after &lt;code&gt;&amp;gt; file&lt;/code&gt; or &lt;code&gt;Out-File&lt;/code&gt;, but the console looked fine: search the script for &lt;code&gt;Write-Host&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Function "printed" a number and the caller got &lt;code&gt;$null&lt;/code&gt;: same root cause — host UI is not the success stream.&lt;/li&gt;
&lt;li&gt;Need host text in a file on purpose: redirect stream 6 (&lt;code&gt;6&amp;gt;&lt;/code&gt;), or stop using &lt;code&gt;Write-Host&lt;/code&gt; for that data.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Practice the stream split hands-on
&lt;/h2&gt;

&lt;p&gt;Reading about streams is easy; muscle memory under a broken log is harder. &lt;a href="https://windows-cli.arnost.org/en" rel="noopener noreferrer"&gt;CMD Master's free interactive browser terminal for PowerShell, CMD, and Bash&lt;/a&gt; lets you run these capture and redirect experiments with no install, until empty logs stop surprising you.&lt;/p&gt;

&lt;p&gt;What was the last script that looked busy on screen and wrote a zero-byte log?&lt;/p&gt;

</description>
      <category>powershell</category>
      <category>scripting</category>
      <category>windows</category>
      <category>beginners</category>
    </item>
    <item>
      <title>PowerShell Get-Content vs Get-Content -Raw: The Real Difference (and Why Your -replace Misses Line Breaks)</title>
      <dc:creator>arnostorg</dc:creator>
      <pubDate>Mon, 02 Nov 2026 08:27:02 +0000</pubDate>
      <link>https://dev.to/arnostorg/powershell-get-content-vs-get-content-raw-the-real-difference-and-why-your-replace-misses-line-3pob</link>
      <guid>https://dev.to/arnostorg/powershell-get-content-vs-get-content-raw-the-real-difference-and-why-your-replace-misses-line-3pob</guid>
      <description>&lt;p&gt;You write a perfectly good regex, run it against a config file with &lt;code&gt;-replace&lt;/code&gt;, and nothing changes. No error, no warning, just the same file back. Nine times out of ten the regex is fine. The problem is that &lt;code&gt;Get-Content&lt;/code&gt; didn't hand you a file, it handed you a list of lines.&lt;/p&gt;

&lt;p&gt;This post is about the single switch that changes that: &lt;code&gt;-Raw&lt;/code&gt;. What it actually returns, when you need it, and the two or three places where it quietly bites you.&lt;/p&gt;

&lt;p&gt;Everything below was tested in PowerShell 7.4 (7.4.6), and every output block is pasted from a real run.&lt;/p&gt;

&lt;h2&gt;
  
  
  The setup
&lt;/h2&gt;

&lt;p&gt;A tiny config file to play with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;Set-Content&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Path&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;\app.conf&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Value&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;@(&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s1"&gt;'server=web01'&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s1"&gt;'port=8080'&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s1"&gt;'# old block'&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s1"&gt;'debug=true'&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s1"&gt;'verbose=true'&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The real difference: many strings vs one string
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$lines&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Get-Content&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Path&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;\app.conf&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="nv"&gt;$whole&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Get-Content&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Path&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;\app.conf&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Raw&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="nv"&gt;$lines&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetType&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Name&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="nv"&gt;$lines&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Count&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="nv"&gt;$whole&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetType&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Name&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="nv"&gt;$whole&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Length&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Object[]
5
String
59
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By default, &lt;code&gt;Get-Content&lt;/code&gt; emits &lt;strong&gt;one string per line&lt;/strong&gt; down the pipeline, and when you capture it in a variable you get an array. The newline characters are gone; they were used as separators.&lt;/p&gt;

&lt;p&gt;With &lt;code&gt;-Raw&lt;/code&gt;, you get &lt;strong&gt;one single string&lt;/strong&gt; containing the whole file, newlines included. The &lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-content" rel="noopener noreferrer"&gt;Get-Content docs&lt;/a&gt; describe it exactly that way: &lt;code&gt;-Raw&lt;/code&gt; ignores newline characters and returns the entire contents of a file in one string with the newlines preserved.&lt;/p&gt;

&lt;p&gt;That one detail explains every "weird" behavior below.&lt;/p&gt;

&lt;p&gt;Quick gut check: indexing.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$lines&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="nv"&gt;$whole&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;server=web01
s
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;$lines[0]&lt;/code&gt; is the first line. &lt;code&gt;$whole[0]&lt;/code&gt; is the first &lt;strong&gt;character&lt;/strong&gt;, because indexing a string gives you a &lt;code&gt;[char]&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why your multi-line -replace does nothing
&lt;/h2&gt;

&lt;p&gt;Say you want to delete the &lt;code&gt;# old block&lt;/code&gt; comment together with the line under it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$pattern&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'# old block\r?\ndebug=true\r?\n'&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$lines&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;-replace&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;$pattern&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;-join&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;`n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="s2"&gt;"=== with -Raw ==="&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="nv"&gt;$whole&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;-replace&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;$pattern&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;server=web01
port=8080
# old block
debug=true
verbose=true
=== with -Raw ===
server=web01
port=8080
verbose=true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Without &lt;code&gt;-Raw&lt;/code&gt;, &lt;code&gt;-replace&lt;/code&gt; runs against each line separately. No single line contains a newline, so a pattern that spans a line break can never match. With &lt;code&gt;-Raw&lt;/code&gt;, the regex sees the whole file and the newlines in it, so it works.&lt;/p&gt;

&lt;p&gt;(I use &lt;code&gt;\r?\n&lt;/code&gt; so the same pattern works on Windows CRLF files and LF files.)&lt;/p&gt;

&lt;p&gt;If you already have the lines, you can glue them back together first:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Get-Content&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;\app.conf&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;-join&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;`n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;-replace&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;$pattern&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;server=web01
port=8080
verbose=true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That works, but &lt;code&gt;-Raw&lt;/code&gt; is cleaner and keeps the original line endings instead of forcing &lt;code&gt;`n&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Counting lines: .Count lies to you with -Raw
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Get-Content&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;\app.conf&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Count&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Get-Content&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;\app.conf&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Raw&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Count&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Get-Content&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;\app.conf&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Measure-Object&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Line&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Lines&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Get-Content&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;\app.conf&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Raw&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Measure-Object&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Line&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Lines&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;5
1
5
5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;.Count&lt;/code&gt; on the raw version is &lt;code&gt;1&lt;/code&gt;, because it's one object. If a script does &lt;code&gt;if ((Get-Content $f -Raw).Count -gt 100)&lt;/code&gt;, that check will never be true. &lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/measure-object" rel="noopener noreferrer"&gt;Measure-Object&lt;/a&gt; with &lt;code&gt;-Line&lt;/code&gt; counts lines inside the text, so it gives the same answer either way.&lt;/p&gt;

&lt;p&gt;The same thing happens with &lt;code&gt;Select-String&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Get-Content&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;\app.conf&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Select-String&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'true'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Count&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Get-Content&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;\app.conf&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Raw&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Select-String&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'true'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Count&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="n"&gt;Get-Content&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;\app.conf&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Select-String&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'true'&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;ForEach-Object&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="bp"&gt;$_&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LineNumber&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;: &lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="bp"&gt;$_&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Line&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2
1
4: debug=true
5: verbose=true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Line by line, you get one match per matching line, with useful line numbers. With &lt;code&gt;-Raw&lt;/code&gt;, the whole file is one "line", so you get one match. For grep-style searching, skip &lt;code&gt;-Raw&lt;/code&gt;, or just use &lt;code&gt;Select-String -Path&lt;/code&gt; directly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Small trap: a one-line file isn't an array
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;Set-Content&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;\one.txt&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Value&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'only line'&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Get-Content&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;\one.txt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetType&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Name&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Get-Content&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;\one.txt&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;@(&lt;/span&gt;&lt;span class="err"&gt;Get-Content&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;.\one.txt&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;String
o
only line
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even without &lt;code&gt;-Raw&lt;/code&gt;, a file with a single line comes back as a plain string, not an array. &lt;code&gt;[0]&lt;/code&gt; then gives you a character. Wrap the call in &lt;code&gt;@( )&lt;/code&gt; when your code expects an array.&lt;/p&gt;

&lt;h2&gt;
  
  
  Round-tripping: Set-Content adds a trailing newline
&lt;/h2&gt;

&lt;p&gt;You read a file with &lt;code&gt;-Raw&lt;/code&gt;, change something, write it back, and Git shows a diff on the last line. Here's why:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$before&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Get-Item&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;\app.conf&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Length&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="nv"&gt;$c&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Get-Content&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;\app.conf&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Raw&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="n"&gt;Set-Content&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;\copy1.conf&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Value&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;$c&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;Set-Content&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;\copy2.conf&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Value&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;$c&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-NoNewline&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="s2"&gt;"original: &lt;/span&gt;&lt;span class="nv"&gt;$before&lt;/span&gt;&lt;span class="s2"&gt; bytes"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="s2"&gt;"Set-Content:            &lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Get-Item&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;\copy1.conf&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;.Length) bytes"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="s2"&gt;"Set-Content -NoNewline: &lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Get-Item&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;\copy2.conf&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;.Length) bytes"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="nv"&gt;$c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;EndsWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;`n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;original: 59 bytes
Set-Content:            60 bytes
Set-Content -NoNewline: 59 bytes
True
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The raw string already ends with the file's final newline. &lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/set-content" rel="noopener noreferrer"&gt;Set-Content&lt;/a&gt; then adds its own newline after the value, so the file grows by one line break every time you round-trip it. &lt;code&gt;-NoNewline&lt;/code&gt; writes the string exactly as it is.&lt;/p&gt;

&lt;p&gt;The safe pattern for in-place edits:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$c&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Get-Content&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;\app.conf&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Raw&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="nv"&gt;$c&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;$c&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;-replace&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'port=8080'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'port=9090'&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;Set-Content&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;\app.conf&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Value&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;$c&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-NoNewline&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  JSON: use -Raw and stop thinking about it
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="sh"&gt;@'
{
  "name": "web01",
  "ports": [80, 443]
}
'@&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Set-Content&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;\server.json&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Get-Content&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;\server.json&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Count&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="nv"&gt;$cfg&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Get-Content&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;\server.json&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Raw&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ConvertFrom-Json&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="nv"&gt;$cfg&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;name&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="nv"&gt;$cfg&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ports&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;-join&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;', '&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;4
web01
80, 443
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Without &lt;code&gt;-Raw&lt;/code&gt;, a pretty-printed JSON file arrives as 4 separate fragments, none of which is valid JSON by itself. With &lt;code&gt;-Raw&lt;/code&gt;, &lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/convertfrom-json" rel="noopener noreferrer"&gt;ConvertFrom-Json&lt;/a&gt; gets the whole document in one piece. That's exactly the pattern Microsoft Learn uses in its own example (&lt;code&gt;Get-Content -Raw JsonFile.json | ConvertFrom-Json&lt;/code&gt;), and the docs show the same example for &lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/convertfrom-json?view=powershell-5.1" rel="noopener noreferrer"&gt;Windows PowerShell 5.1&lt;/a&gt;. If your scripts run on both 5.1 and 7, &lt;code&gt;-Raw&lt;/code&gt; is the habit to keep.&lt;/p&gt;

&lt;h2&gt;
  
  
  Big files: -Raw vs default vs -ReadCount
&lt;/h2&gt;

&lt;p&gt;Test file: 200,000 log lines, about 8 MB.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;200000&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ForEach-Object&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2026-11-02 08:00:00 INFO request &lt;/span&gt;&lt;span class="bp"&gt;$_&lt;/span&gt;&lt;span class="s2"&gt; ok"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;Set-Content&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;\big.log&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="bp"&gt;$null&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Get-Content&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;\big.log&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Raw&lt;/span&gt;&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="c"&gt;# warm-up&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="kr"&gt;foreach&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$i&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kr"&gt;in&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nv"&gt;$d&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Measure-Command&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="bp"&gt;$null&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Get-Content&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;\big.log&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;TotalMilliseconds&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nv"&gt;$r&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Measure-Command&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="bp"&gt;$null&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Get-Content&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;\big.log&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-ReadCount&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;1000&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;TotalMilliseconds&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nv"&gt;$w&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Measure-Command&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="bp"&gt;$null&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Get-Content&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;\big.log&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Raw&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;TotalMilliseconds&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="s2"&gt;"run {0}: default {1,5:N0} ms | -ReadCount 1000 {2,4:N0} ms | -Raw {3,4:N0} ms"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-f&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;$i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nv"&gt;$d&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nv"&gt;$r&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nv"&gt;$w&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;run 1: default 1,339 ms | -ReadCount 1000  159 ms | -Raw   54 ms
run 2: default 1,003 ms | -ReadCount 1000   10 ms | -Raw  109 ms
run 3: default   999 ms | -ReadCount 1000   10 ms | -Raw  107 ms
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your numbers will differ, but the shape is consistent: the default (one pipeline object per line) is by far the slowest. &lt;code&gt;-Raw&lt;/code&gt; is fast, but it loads the entire file into memory as one string, which is fine for configs and not great for a 4 GB log.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;-ReadCount&lt;/code&gt; is the middle ground. It sends lines down the pipeline in batches, so you keep line-by-line logic without paying per-line overhead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$n&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;Get-Content&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;\big.log&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-ReadCount&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;1000&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ForEach-Object&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nv"&gt;$n&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="kr"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$n&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;-eq&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"batch type: &lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="bp"&gt;$_&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetType&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="s2"&gt;.Name), size: &lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="bp"&gt;$_&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Count&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="s2"&gt;"batches: &lt;/span&gt;&lt;span class="nv"&gt;$n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;batch type: Object[], size: 1000
batches: 200
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that &lt;code&gt;$_&lt;/code&gt; is now an &lt;strong&gt;array of 1000 lines&lt;/strong&gt;, not a single line. Code written for the default mode (&lt;code&gt;$_ -match 'ERROR'&lt;/code&gt;) still runs but behaves differently on an array (it filters instead of returning &lt;code&gt;$true&lt;/code&gt;/&lt;code&gt;$false&lt;/code&gt;), so adjust it deliberately. For huge logs where you only need the end, &lt;code&gt;-Tail&lt;/code&gt; is a better tool anyway.&lt;/p&gt;

&lt;h2&gt;
  
  
  Which one to use
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;You want to...&lt;/th&gt;
&lt;th&gt;Use&lt;/th&gt;
&lt;th&gt;Why&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Loop over lines, filter, grep&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;Get-Content&lt;/code&gt; (default)&lt;/td&gt;
&lt;td&gt;One object per line, line numbers work&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Regex or &lt;code&gt;-replace&lt;/code&gt; across line breaks&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Get-Content -Raw&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The regex needs to see the newlines&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Parse JSON / XML / a whole document&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Get-Content -Raw&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Parser needs the full text in one piece&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Edit a file and write it back unchanged&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;-Raw&lt;/code&gt; + &lt;code&gt;Set-Content -NoNewline&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Avoids the extra trailing newline&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Count lines&lt;/td&gt;
&lt;td&gt;default &lt;code&gt;.Count&lt;/code&gt; or &lt;code&gt;Measure-Object -Line&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;.Count&lt;/code&gt; on &lt;code&gt;-Raw&lt;/code&gt; is always 1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Process a big file line by line, faster&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Get-Content -ReadCount 1000&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Batches lines, far less pipeline overhead&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Read the end of a huge log&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Get-Content -Tail 50&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Doesn't need the whole file&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Common pitfalls, in one place
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;.Count&lt;/code&gt; is 1&lt;/strong&gt; after &lt;code&gt;-Raw&lt;/code&gt;. It's one string.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;$x[0]&lt;/code&gt; is a character&lt;/strong&gt; when &lt;code&gt;$x&lt;/code&gt; is a string, which happens with &lt;code&gt;-Raw&lt;/code&gt; and with one-line files. Use &lt;code&gt;@( )&lt;/code&gt; if you need an array.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Multi-line regex silently fails&lt;/strong&gt; without &lt;code&gt;-Raw&lt;/code&gt;, because each line is matched separately.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Set-Content adds a trailing newline.&lt;/strong&gt; Write raw content back with &lt;code&gt;-NoNewline&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-ReadCount&lt;/code&gt; changes &lt;code&gt;$_&lt;/code&gt;&lt;/strong&gt; from a line into an array of lines.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-Raw&lt;/code&gt; loads everything into memory.&lt;/strong&gt; Fine for configs, think twice for multi-GB logs.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you want to try these without touching real files, you can &lt;a href="https://windows-cli.arnost.org/en" rel="noopener noreferrer"&gt;practice PowerShell file commands in a free browser terminal&lt;/a&gt;. No install, nothing to break.&lt;/p&gt;

&lt;p&gt;Docs used for this post:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-content" rel="noopener noreferrer"&gt;Get-Content&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/set-content" rel="noopener noreferrer"&gt;Set-Content&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/convertfrom-json" rel="noopener noreferrer"&gt;ConvertFrom-Json&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/measure-object" rel="noopener noreferrer"&gt;Measure-Object&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_comparison_operators" rel="noopener noreferrer"&gt;about_Comparison_Operators (-replace)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What's the script that bit you because &lt;code&gt;Get-Content&lt;/code&gt; returned lines instead of a file? Drop it in the comments.&lt;/p&gt;

</description>
      <category>powershell</category>
      <category>scripting</category>
      <category>windows</category>
      <category>beginners</category>
    </item>
    <item>
      <title>PowerShell Select-Object vs Format-Table: The Real Difference (and Why Your CSV Is Full of GUIDs)</title>
      <dc:creator>arnostorg</dc:creator>
      <pubDate>Fri, 30 Oct 2026 08:24:18 +0000</pubDate>
      <link>https://dev.to/arnostorg/powershell-select-object-vs-format-table-the-real-difference-and-why-your-csv-is-full-of-guids-o1m</link>
      <guid>https://dev.to/arnostorg/powershell-select-object-vs-format-table-the-real-difference-and-why-your-csv-is-full-of-guids-o1m</guid>
      <description>&lt;p&gt;&lt;code&gt;Select-Object&lt;/code&gt; and &lt;code&gt;Format-Table&lt;/code&gt; both let you pick which columns you see, and in the console they look almost identical. Then you pipe the result into &lt;code&gt;Export-Csv&lt;/code&gt; and get a file full of GUIDs instead of file names. That is not a bug in &lt;code&gt;Export-Csv&lt;/code&gt;. It is the one real difference between these two cmdlets, and once you see it you stop making the mistake.&lt;/p&gt;

&lt;p&gt;Everything below was run in PowerShell 7.4.6. The behavior is the same in Windows PowerShell 5.1.&lt;/p&gt;

&lt;h2&gt;
  
  
  The shortest honest comparison
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;Select-Object&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;Format-Table&lt;/code&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;What it outputs&lt;/td&gt;
&lt;td&gt;Real objects with the properties you picked&lt;/td&gt;
&lt;td&gt;Formatting instructions for the host&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Can you sort, filter, or export afterwards?&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;No, not in any useful way&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Where it belongs in a pipeline&lt;/td&gt;
&lt;td&gt;Anywhere&lt;/td&gt;
&lt;td&gt;Last, or right before &lt;code&gt;Out-File&lt;/code&gt; / &lt;code&gt;Out-String&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Typical job&lt;/td&gt;
&lt;td&gt;Shape data for the next command&lt;/td&gt;
&lt;td&gt;Make data readable for a human&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;If you remember one rule, remember the one Microsoft Learn calls &lt;a href="https://learn.microsoft.com/en-us/powershell/scripting/learn/ps101/05-formatting-aliases-providers-comparison" rel="noopener noreferrer"&gt;format right&lt;/a&gt;: formatting cmdlets go at the very end of the line.&lt;/p&gt;

&lt;h2&gt;
  
  
  The bug: Format-Table before Export-Csv
&lt;/h2&gt;

&lt;p&gt;Here is the mistake almost everyone makes once:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;Get-ChildItem&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;C:\Logs\&lt;/span&gt;&lt;span class="o"&gt;*.&lt;/span&gt;&lt;span class="nf"&gt;txt&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;Format-Table&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Length&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;Export-Csv&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;C:\Reports\logs.csv&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-NoTypeInformation&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And here is what actually lands in the CSV:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"ClassId2e4f51ef21dd47e99d3c952918aff9cd","pageHeaderEntry","pageFooterEntry","autosizeInfo","shapeInfo","groupingEntry"
"033ecb2bc07a4d43b5ef94ed5a35d280",,,,"Microsoft.PowerShell.Commands.Internal.Format.TableHeaderInfo",
"9e210fe47d09416682b841769c78b8a3",,,,,
"27c87ef9bbda4f709f6b4002fa4af63c",,,,,
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No file names, no sizes. And &lt;code&gt;Export-Csv&lt;/code&gt; is not wrong to stay quiet. As Mark Wragg pointed out in the comments, &lt;code&gt;Format-Table&lt;/code&gt; still outputs real PowerShell objects (&lt;code&gt;FormatStartData&lt;/code&gt;, &lt;code&gt;GroupStartData&lt;/code&gt;, &lt;code&gt;FormatEntryData&lt;/code&gt;, and so on). &lt;code&gt;Export-Csv&lt;/code&gt; is built to serialize whatever objects it receives, so it writes the properties of those formatting records instead of your data. That is why Microsoft handles it with a warning in the &lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/export-csv" rel="noopener noreferrer"&gt;Export-Csv documentation&lt;/a&gt; and not a blocked command: "Do not format objects before sending them to the Export-Csv cmdlet. If Export-Csv receives formatted objects the CSV file contains the format properties rather than the object properties."&lt;/p&gt;

&lt;p&gt;The fix is one word:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;Get-ChildItem&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;C:\Logs\&lt;/span&gt;&lt;span class="o"&gt;*.&lt;/span&gt;&lt;span class="nf"&gt;txt&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;Select-Object&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Length&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;Export-Csv&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;C:\Reports\logs.csv&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-NoTypeInformation&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"Name","Length"
"log1.txt","101"
"log2.txt","201"
"log3.txt","301"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;-NoTypeInformation&lt;/code&gt; is only needed in Windows PowerShell 5.1. PowerShell 7 leaves the &lt;code&gt;#TYPE&lt;/code&gt; header out by default, and the switch is harmless there.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prove it with Get-Member
&lt;/h2&gt;

&lt;p&gt;You do not have to take anyone's word for it. Ask PowerShell what type comes out of each cmdlet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;Get-ChildItem&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;*.&lt;/span&gt;&lt;span class="nf"&gt;txt&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Format-Table&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Length&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Get-Member&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Select-Object&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-First&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-ExpandProperty&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;TypeName&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="c"&gt;# Microsoft.PowerShell.Commands.Internal.Format.FormatStartData&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="n"&gt;Get-ChildItem&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;*.&lt;/span&gt;&lt;span class="nf"&gt;txt&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Select-Object&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Length&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Get-Member&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Select-Object&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-First&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-ExpandProperty&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;TypeName&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="c"&gt;# Selected.System.IO.FileInfo&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Select-Object&lt;/code&gt; gives you new objects that still carry &lt;code&gt;Name&lt;/code&gt; and &lt;code&gt;Length&lt;/code&gt; as real properties. &lt;code&gt;Format-Table&lt;/code&gt; gives you internal formatting records that only the console knows how to draw.&lt;/p&gt;

&lt;h2&gt;
  
  
  The quieter bug: sorting and filtering after Format-Table
&lt;/h2&gt;

&lt;p&gt;The CSV case is loud. These two are silent, which makes them worse.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Looks fine, sorts nothing&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;Get-ChildItem&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;*.&lt;/span&gt;&lt;span class="nf"&gt;txt&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Format-Table&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Length&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Sort-Object&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Length&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Descending&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is no error. The table prints in the original order, because &lt;code&gt;Sort-Object&lt;/code&gt; is sorting formatting records that have no &lt;code&gt;Length&lt;/code&gt; property.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Get-ChildItem&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;*.&lt;/span&gt;&lt;span class="nf"&gt;txt&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Format-Table&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Length&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Where-Object&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Length&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;-gt&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;200&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Measure-Object&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Count&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="c"&gt;# 0&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Get-ChildItem&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;*.&lt;/span&gt;&lt;span class="nf"&gt;txt&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Select-Object&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Length&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Where-Object&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Length&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;-gt&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;200&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Measure-Object&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Count&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="c"&gt;# 4&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Same input, same filter, completely different answer. If a script "finds nothing" and there is a &lt;code&gt;Format-*&lt;/code&gt; anywhere before the filter, that is your first suspect.&lt;/p&gt;

&lt;h2&gt;
  
  
  The right order: filter, sort, select, then format
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;Get-ChildItem&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;C:\Logs&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-File&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;Where-Object&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Length&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;-gt&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;1MB&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;Sort-Object&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Length&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Descending&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;Select-Object&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-First&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;10&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Length&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;LastWriteTime&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;Format-Table&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-AutoSize&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Read it left to right: narrow the data, order it, keep the columns you need, and only then make it pretty. Swap the last line for &lt;code&gt;Export-Csv&lt;/code&gt;, &lt;code&gt;ConvertTo-Json&lt;/code&gt;, or another cmdlet and the pipeline still works, because everything before it is real data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table on screen and CSV in one pipeline: Tee-Object
&lt;/h2&gt;

&lt;p&gt;Want to see the table and save the CSV in one go? Mark Wragg suggested &lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/tee-object" rel="noopener noreferrer"&gt;&lt;code&gt;Tee-Object&lt;/code&gt;&lt;/a&gt;, which sends output in two directions: it stores it in a file or variable and also passes it down the pipeline. Use &lt;code&gt;-Variable&lt;/code&gt;, not &lt;code&gt;-FilePath&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;Get-ChildItem&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;C:\Logs\&lt;/span&gt;&lt;span class="o"&gt;*.&lt;/span&gt;&lt;span class="nf"&gt;txt&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;Select-Object&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Length&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;Tee-Object&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Variable&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;rows&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;Format-Table&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-AutoSize&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="nv"&gt;$rows&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Export-Csv&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;C:\Reports\logs.csv&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-NoTypeInformation&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Name     Length
----     ------
log1.txt    101
log2.txt    201
log3.txt    301
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"Name","Length"
"log1.txt","101"
"log2.txt","201"
"log3.txt","301"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why not &lt;code&gt;Tee-Object -FilePath logs.csv&lt;/code&gt;? Internally &lt;code&gt;Tee-Object&lt;/code&gt; uses &lt;code&gt;Out-File&lt;/code&gt; for the file, so the file gets the rendered table text, not comma-separated values. Real objects stay in &lt;code&gt;$rows&lt;/code&gt;, and &lt;code&gt;Export-Csv&lt;/code&gt; turns them into a proper CSV.&lt;/p&gt;

&lt;h2&gt;
  
  
  Calculated columns belong in Select-Object too
&lt;/h2&gt;

&lt;p&gt;Both cmdlets accept &lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_calculated_properties" rel="noopener noreferrer"&gt;calculated properties&lt;/a&gt;, so it is tempting to build a "SizeKB" column inside &lt;code&gt;Format-Table&lt;/code&gt;. Build it in &lt;code&gt;Select-Object&lt;/code&gt; instead and it survives the export:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;Get-ChildItem&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;C:\Logs\&lt;/span&gt;&lt;span class="o"&gt;*.&lt;/span&gt;&lt;span class="nf"&gt;txt&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;Select-Object&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;@{&lt;/span&gt;&lt;span class="nx"&gt;Name&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'SizeKB'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Expression&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;math&lt;/span&gt;&lt;span class="p"&gt;]::&lt;/span&gt;&lt;span class="n"&gt;Round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;$_&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Length&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;/&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;1KB&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}}&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;|&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nx"&gt;Export&lt;/span&gt;&lt;span class="err"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;Csv&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;C&lt;/span&gt;&lt;span class="err"&gt;:\&lt;/span&gt;&lt;span class="nx"&gt;Reports&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nx"&gt;logs&lt;/span&gt;&lt;span class="err"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;kb&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;csv&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;NoTypeInformation&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"Name","SizeKB"
"log1.txt","0.1"
"log2.txt","0.2"
"log3.txt","0.29"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  When Format-Table is exactly right
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Format-Table&lt;/code&gt; is not the villain. It is the right tool whenever a human is the last consumer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Readable console output&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;Get-Service&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Where-Object&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Status&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;-eq&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'Running'&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Format-Table&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;DisplayName&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-AutoSize&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="c"&gt;# Readable text report for a ticket or email&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;Get-ChildItem&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;C:\Logs&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-File&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Format-Table&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Length&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-AutoSize&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Out-File&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;C:\Reports\logs.txt&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Out-File&lt;/code&gt; and &lt;code&gt;Out-String&lt;/code&gt; understand formatting records, so a formatted table written to a &lt;code&gt;.txt&lt;/code&gt; file looks exactly like it did on screen. See the &lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/format-table" rel="noopener noreferrer"&gt;Format-Table reference&lt;/a&gt; for &lt;code&gt;-AutoSize&lt;/code&gt;, &lt;code&gt;-Wrap&lt;/code&gt;, and &lt;code&gt;-GroupBy&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Two Select-Object switches worth knowing
&lt;/h2&gt;

&lt;p&gt;From the &lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-object" rel="noopener noreferrer"&gt;Select-Object reference&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Just the values, not objects with one property&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;Get-ChildItem&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;C:\Logs\&lt;/span&gt;&lt;span class="o"&gt;*.&lt;/span&gt;&lt;span class="nf"&gt;txt&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Select-Object&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-ExpandProperty&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Name&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="c"&gt;# Top N after sorting&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;Get-ChildItem&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;C:\Logs&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-File&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Sort-Object&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Length&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Descending&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Select-Object&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-First&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;5&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Length&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;-ExpandProperty&lt;/code&gt; is the one to reach for when the next command wants plain strings, like a list of names to feed into &lt;code&gt;Remove-Item&lt;/code&gt; or &lt;code&gt;Copy-Item&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Quick checklist
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Next step is a cmdlet, a file format, or another script: use &lt;code&gt;Select-Object&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Next step is your eyes or a &lt;code&gt;.txt&lt;/code&gt; report: use &lt;code&gt;Format-Table&lt;/code&gt;, and put it last.&lt;/li&gt;
&lt;li&gt;Export full of GUIDs, sort that does nothing, or filter that returns zero: look for a &lt;code&gt;Format-*&lt;/code&gt; too early in the pipeline.&lt;/li&gt;
&lt;li&gt;Need a computed column you can export: build it in &lt;code&gt;Select-Object&lt;/code&gt; with a calculated property.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Practice the pipeline order hands-on
&lt;/h2&gt;

&lt;p&gt;Reading about pipeline order is easy; getting it right under pressure takes reps. &lt;a href="https://windows-cli.arnost.org/en" rel="noopener noreferrer"&gt;CMD Master's free interactive PowerShell, CMD, and Bash practice in your browser&lt;/a&gt; lets you run real commands in a browser terminal with no install, so you can drill filter-sort-select-format until it is automatic.&lt;/p&gt;

&lt;p&gt;What is the strangest thing you have found in a CSV that went through &lt;code&gt;Format-Table&lt;/code&gt; first?&lt;/p&gt;

</description>
      <category>powershell</category>
      <category>scripting</category>
      <category>windows</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
