<pre><code>Function Clean-InvalidFileNameChars {
  param(
    [Parameter(Mandatory=$true,
      Position=0,
      ValueFromPipeline=$true,
      ValueFromPipelineByPropertyName=$true)]
    [String]$Name
  )

  $invalidChars = [IO.Path]::GetInvalidFileNameChars() -join ''
  $re = "[{0}]" -f [RegEx]::Escape($invalidChars)
  $res=($Name -replace $re)
  return $res.Substring(0, [math]::Min(260, $res.Length))
}

Function Clean-InvalidPathChars {
  param(
    [Parameter(Mandatory=$true,
      Position=0,
      ValueFromPipeline=$true,
      ValueFromPipelineByPropertyName=$true)]
    [String]$Name
  )

  $invalidChars = [IO.Path]::GetInvalidPathChars() -join ''
  $re = "[{0}]" -f [RegEx]::Escape($invalidChars)
  $res=($Name -replace $re)
  return $res.Substring(0, [math]::Min(248, $res.Length))
}


$rootpath="c:\temp2"

$rootpathresult="c:\tempresult"

$template=@'
[3]  arXiv:1611.00057 [pdf, ps, other] 
Title: {title*:Holomorphy of adjoint $L$ functions for quasisplit A2} 
Authors: Joseph Hundley 
Comments: 18 pages 
Subjects: {subject:Number Theory (math.NT)} 
[4]  arXiv:1611.00066 [pdf, other] 
Title: {title*:Many Haken Heegaard splittings} 
Authors: Alessandro Sisto 
Comments: 12 pages, 3 figures 
Subjects: {subject:Geometric Topology (math.GT)} 
[5]  arXiv:1611.00067 [pdf, ps, other] 
Title: {title*:Subsumed homoclinic connections and infinitely many coexisting attractors in piecewise-linear maps} 
Authors: David J.W. Simpson, Christopher P. Tuffley 
Subjects: {subject:Dynamical Systems (math.DS)} 
[21]  arXiv:1611.00114 [pdf, ps, other] 
Title: {title*:Faces of highest weight modules and the universal Weyl polyhedron} 
Authors: Gurbir Dhillon, Apoorva Khare 
Comments: We recall preliminaries and results from the companion paper arXiv:1606.09640 
Subjects: {subject:Representation Theory (math.RT)}; Combinatorics (math.CO); Metric Geometry (math.MG)
'@

#extract utils data and clean 
$listbook=gci $rootpath -File -filter *.pdf | foreach { New-Object psobject -Property @{file=$_.fullname; books= ((iwr "https://arxiv.org/abs/$($_.BaseName)").ParsedHtml.body.outerText | ConvertFrom-String -TemplateContent $template)}} | select file -ExpandProperty books |  select file,  @{N="Subject";E={Clean-InvalidPathChars $_.subject}}, @{N="Title";E={Clean-InvalidFileNameChars $_.title}}  



#build dirs and copy+rename file
$listbook | %{$newpath="$rootpathresult\$($_.subject)"; New-Item -ItemType Directory -Path "$newpath" -Force;  Copy-Item $_.file "$newpath\$($_.title).pdf" -Force}
</code></pre>
Loop and join 2 functions to move files in folders by category & rename by original title
<p>For <em>matching</em> category with filename I use this code</p>

<pre><code> gci *.pdf | foreach { (iwr "https://arxiv.org/abs/$($_.BaseName)")`
 -match 'primary-subject"&gt;(.*?)&lt;/span&gt;'; $matches[1] }
</code></pre>

<p>To have idea what I mean <a href="https://i.imgur.com/57KjJr6.png" rel="nofollow noreferrer">http://i.imgur.com/57KjJr6.png</a></p>

<p>To <em>rename</em> indipendently: I can use that but this not useful because I process folder for folder and this take long time (number of folders is high number)</p>

<pre><code>#All PDFs | Rename { query Arxiv for the abstract by filename, use the page title + ".pdf"}

    Get-ChildItem *.pdf | Rename-Item -NewName { 
        $title = (Invoke-WebRequest "https://arxiv.org/abs/$($_.BaseName)").parsedhtml.title
        $title = $title -replace '[\\/:\*\?"&lt;&gt;\|]', '-'  # replace forbidden characters
        "$title.pdf"                                     # in filenames with -
    }
</code></pre>

<p>I should make this folders like this (without <em>[folder]</em>)</p>

<pre><code>[folder] Information Theory (cs.IT)
[folder] Number Theory (math.NT)
....
</code></pre>

<p>I try to <em>join</em> 2 operations:</p>

<p>MOVING by Subject</p>

<pre><code>[folder] Geometric Topology (cs.IT)
        |
        |__ [file] 1611.00066
        |__ [file] .....

[folder] Number Theory (math.NT)
    |
    |__ [file] 1611.00057
</code></pre>

<p>and RENAMING by Title</p>

<pre><code>[folder] Geometric Topology (cs.IT)
        |
        |__ [file] 1611.00066
        |__ [file] .....

[folder] Number Theory (math.NT)
        |
        |__ [file] 1611.00057
</code></pre>

<p>For loop and join operation I make a <em>.ps1</em> file. I insert this code but don't work</p>

<pre><code>$res=Invoke-WebRequest "https://arxiv.org/abs/$($_.BaseName"
   $rootpath="c:\temp"

   Function Clean-InvalidFileNameChars {
      param(
        [Parameter(Mandatory=$true,
          Position=0,
          ValueFromPipeline=$true,
          ValueFromPipelineByPropertyName=$true)]
        [String]$Name
      )

      $invalidChars = [IO.Path]::GetInvalidFileNameChars() -join ''
      $re = "[{0}]" -f [RegEx]::Escape($invalidChars)
      $res=($Name -replace $re)
      return $res.Substring(0, [math]::Min(260, $res.Length))
    }

    Function Clean-InvalidPathChars {
      param(
        [Parameter(Mandatory=$true,
          Position=0,
          ValueFromPipeline=$true,
          ValueFromPipelineByPropertyName=$true)]
        [String]$Name
      )

      $invalidChars = [IO.Path]::GetInvalidPathChars() -join ''
      $re = "[{0}]" -f [RegEx]::Escape($invalidChars)
      $res=($Name -replace $re)
      return $res.Substring(0, [math]::Min(248, $res.Length))
    }

    gci *.pdf | foreach { (iwr "https://arxiv.org/abs/$($_.BaseName)")`
     -match 'primary-subject"&gt;(.*?)&lt;/span&gt;'; $matches[1] }


    #get date and cut with format template, group by Subject and clean Title and Subject for transformation to dir and file name
    $grousubject=$res.ParsedHtml.body.outerText | ConvertFrom-String -TemplateContent $template | select  @{N="Subject";E={Clean-InvalidPathChars $_.subject}}, @{N="Title";E={Clean-InvalidFileNameChars $_.title}} | group Subject 

    #create dir and files
    $grousubject | %{$path= "$rootpath\$($_.Name)" ; $_.group.title | %{New-Item -ItemType File -Path "$path\$_" -Force}   }

   Get-ChildItem *.pdf | Rename-Item -NewName { 
            $title = (Invoke-WebRequest "https://arxiv.org/abs/$($_.BaseName)").parsedhtml.title
            $title = $title -replace '[\\/:\*\?"&lt;&gt;\|]', '-'  # replace forbidden characters
            "$title.pdf"                                     # in filenames with -
        }
</code></pre>

<p>My powershell version is 4</p>

<p><strong>EDIT:</strong> Esmeraldo solution works like this <a href="https://i.imgur.com/NEio868.png" rel="nofollow noreferrer">http://i.imgur.com/NEio868.png</a></p>

<p>Thank you</p>