title: Moving files to a new folder inside their parent based on a criteria
body: <p>I am currently working on a project in Powershell, which I am really not super familiar with. I have gotten pretty close, and done some looking around to get me even closer, but I am stumped on this bigtime.</p>
  
  <p>The goal is to recursively search a directory for files prepended with "<code>\_7Y_</code>" (this was done by another script I wrote, based on the files age) and to create a new subdirectory in that files parent and then to move it there. For example, if I have <code>~\desktop\old\\_7Y_OldFile.txt</code>, I want that file to go to <code>~\desktop\old\\_7Y_\\_7Y_OldFile.txt</code> and I want it to do that for every file recursively. </p>
  
  <p>My script currently creates the folders as expected, but selects only one of the new folders to move the item to. I think this is due to the $child variable selecting only that one value, as after it moves the file the script continues but gets errors trying to find the files to move them. Short of manually telling it each parent (this process will eventually be automated), I was wondering what could be done to make a distinction between each $child for the move. </p>
  
  <p>It is also my understanding that I shouldn't need to do a New-Item and then the Move-Item, as long as I use - force, but that has not been my experience either. Any help would be appreciated.</p>
  
  <p>I apologize for the strange formatting and techniques -- I've really been learning powershell by ear as I go. </p>
  
  <pre><code>[CmdletBinding()]
  Param(
  [Parameter(mandatory=$true)]
  [ValidateScript({Test-Path $_ -PathType 'any'})]
  [string] $InputFilePath
  )
  #this sets the filepath parameter to mandatory, so you will need to input 
  your own filepath!
  
  $directoryInfo = Get-ChildItem $InputFilePath -recurse | Measure-Object 
  $7Yno = Get-ChildItem $InputFilePath -recurse | Where-Object {$_.Name -like '_7Y_*.*'} | Measure-Object
  #These are used as the conditions for the if statements
  #directoryInfo gets the number of files in the directory; 7Yno gets the number of files prepended with _7Y_ in the directory
  
  $Confirmation = Read-Host "Are you SURE you want to proceed with this operation? All selected files in the given directory will be moved to new directories! This action is irreversable. Your selected directory is $InputFilePath. Enter 'Yes' to proceed"
  
  if ($Confirmation -eq "Yes") {
  
      if ($directoryInfo.count -gt 0 -and $7Yno.count -gt 0){   #Check: Files in directory and files prepended with _7Y_ in directory.
  
      $children = @((Get-ChildItem $InputFilePath -recurse | 
          Where-Object {$_.Name -like "_7Y_*.*"}).directory.fullname | 
              Get-Unique)
      $Files = @(Get-ChildItem $children -recurse | 
          Where-Object {$_.Name -like "_7Y_*.*"})
  
          foreach($child in $children){
              $7yPath = "$child\_7Y_"
              New-Item -itemtype Directory -path $7yPath -force
          }
          foreach($file in $Files){
              Move-Item $file.fullname -destination $7yPath -force
          }
      }
  
      if ($directoryInfo.count -gt 0 -and $7Yno.count -eq 0){   #Check: Files in directory, but no files prepended with _7Y_ in directory.
      Read-Host "There are no files prepended with _7Y_ in this directory!"
      }
  
      if ($directoryInfo.count -eq 0){                            #Check: No files in directory.
      Read-Host "There are no files in this directory!"
      }
  }
  </code></pre>