title: Powershell - Unzip all zipped files in a directory as well as rename them based on the parent file names
body: <p>I have a folder and set of files like so:</p>
  <ul>
  <li>Root &gt;</li>
  <li>Parent 1 &gt;</li>
  <li>file 1 &gt; export.zip &gt; export.txt</li>
  <li>file 2 &gt; export.zip &gt; export.txt</li>
  <li>file 3 &gt; export.zip &gt; export.txt</li>
  <li>Parent 2 &gt;</li>
  <li>file 1 &gt; export.zip &gt; export.txt</li>
  <li>file 2 &gt; export.zip &gt; export.txt</li>
  <li>file 3 &gt; export.zip &gt; export.txt</li>
  </ul>
  <p>So the root folder contains hundreds of parent folders, each parent folder contains hundreds of files, each file contains <strong>a single zipfile with the title &quot;export&quot;</strong>, and each zipfile contains <strong>a single .txt file, also named &quot;export&quot;</strong>.</p>
  <p>I need to get</p>
  <ol>
  <li>all of those .txt files out of the zipfiles,</li>
  <li>all of those .txt files named something unique using a known method, and</li>
  <li>and moved to a common folder location.</li>
  </ol>
  <p>So far I have a few bits of the process working but nothing that does all three things. I can rename the zipfiles something unique, based on the file and parent names, by doing these two lines:</p>
  <p><code>Get-ChildItem -LiteralPath 'C:\Insert Root Directory here' -Recurse | Rename-Item -NewName { $_.Parent.Name+'_'+$_.Name}</code></p>
  <p>and then</p>
  <p><code>Get-ChildItem -LiteralPath 'C:\Insert Root Directory here' -Filter *.zip -Recurse | Rename-Item -NewName { $_.Directory.Name+'_'+$_.Name}</code></p>
  <p>Then I tried something I found on here to unzip the zipfiles, rename the .txt files they contain, and move them simultaneously to new location, but it is erroring with a &quot;movefileinfoitemunauthorizedaccesserror microsoft.powershell.commands.moveitemcommand&quot; error. I have tried tinkering around with it but I cannot get it to work. If remove everything after &quot;#Move Our Temp Files&quot; the error goes away but the outcomes is still not successful. I get a single .txt file named export in the $TempPath location. :</p>
  <pre><code>$ZipFilesPath = &quot;C:\Insert Root Directory here&quot;;
  #Unzip To Temp Folder Defined Here
  $TempPath = &quot;C:\Insert Location to put .txt files here&quot;
  
  #Create Our Temp File If It Doesnt Exist (Will Be Deleted Again)
  If(!(test-path $TempPath))
  {
        New-Item -ItemType Directory -Force -Path $TempPath
  }
  
  $Shell = New-Object -com Shell.Application
  $Location = $Shell.NameSpace($TempPath)
  $ZipFiles = Get-Childitem $ZipFilesPath -Recurse -Include *.ZIP
  $FileCounter = 1
  
  foreach ($ZipFile in $ZipFiles) {
      #Get The Base Filename without the Filepath
      $ZipFileActualName = [io.path]::GetFileNameWithoutExtension($ZipFile.FullName)
      write-host &quot;File: &quot;  $ZipFileActualName
      $ZipFolder = $Shell.NameSpace($ZipFile.fullname)
      $Location.Copyhere($ZipFolder.items(), 1040)
      #Move Our Temp Files
      $txtFiles = Get-ChildItem $TempPath *.txt
      $txtFiles |% {Move-Item  $_.Fullname &quot;$UnzipPath/$ZipFileActualName.txt&quot;}
      #Move Along to Next File
      $FileCounter++
  }
  </code></pre>
  <p>Can anyone help me achieve the above? Open to any and all ideas. Thanks!</p>