Hello all new guy here.

First off I would like to thank you guys for taking the time to help novice users like myself. I have been doing powershell for about 4 weeks now. I have read Learn PowerShell in a month of Lunches, Learn Powershell Tool making in a month of lunches, Windows Powershell Best Practices and Windows Powershell cookbook. I have taken 3 MVA courses by Jeffrey Snover and Jason Helmick. Those guys are awesome and well spoken. they directed me to this site in one of there courses. Anyway I just wanted to introduce myself and thank you in advance for having patience with me.

here is my latest script it fixes an issue we have with folder redirection. during the process of folder redirection during boot up the computer starts to move the files from the local user folders to the network equivalent some user have files at the threshold of the character limitation what happens when it gets to this issue is everything stops and the system leaves half of there files on the redirected file server and half on the users profile.

To fix this I wrote a script when launched on the users machine retrieves the redirected path from the registry, and the path of each of the stuck user files combines them the way they would be if sent to the file server. Then it gets the length of the string if it exceeds the maximum length it builds the folder structure on root of c:\fldrrdr and moved the offending files while creating a log file of the files moved. it also makes a log file of any files we have in our filter.

Here is the code if anyone sees any mistakes or a simpler way to do it please let me know I love to learn new things.

 #requires -Version 3
Set-StrictMode -Version Latest
set-location $env:USERPROFILE
[int]$maxpathlength = 255
$paths = ("Favorites" , "Documents" , "Desktop")

if(!(test-path c:\FLDRRDR)){MD c:\FLDRRDR}

 # Setting Folder Redirection Variables
$frd = Get-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders' 

 # Finds all files in the user directory that violates the charater limitation then moves the files to c:\FLDRRDR retaining the folder structure.
Foreach($path in $paths){
    if(test-path $path){
        $files = Get-ChildItem -Path $path -Recurse
            Foreach($file in $files){
                $userfile = ($file.FullName).Replace("$env:USERPROFILE","")
                $newdir = ($userfile).Replace($($file.Name) , "")
                if($file.FullName -like "*.pst" -or $file.FullName -like "*.ost" -or $file.FullName -like "*.oft" -or $file.FullName -like "*.pab" -or $file.FullName -like "*.msg"){$file.FullName | Out-File c:\Long-File-Check\Outlook-Files.txt -Append}
                if($remotepath.Length + $userfile.Length -gt $maxpathlength) {
                    $file.FullName | Out-File c:\FLDRRDR\Long-File-Names-Log.txt -Append
                    if(!(test-path "C:\FLDRRDR$($newdir)" )){MD "C:\FLDRRDR$($newdir)"}
                    if(test-path "C:\FLDRRDR$($newdir)" ){move-item -Path "$($file.FullName)" -Destination "C:\FLDRRDR$($newdir)"}

Welcome to Powershell.org! A couple of things:

  • Check out Special Folders to get the path to Desktop, Favorites, etc. in the context of the user. You don't have to do a registry query or the replace of the user profile.
  • Rather than pull all files and directories and then filter with an If to see if it's *.pst, *.ost, etc., you should filter with Get-ChildItem to only return those files. Look at the -Include or -Filter switches.

Wow I just tested [Environment]::GetFolderPath(‘MyDocuments’) and it did in fact return the redirected folder path hmmm that is awesome. Thank you so much… I was able to remove one lie of code

here is the new piece of code :slight_smile:

as for the file filter i’m already looping through all the files to calculate the new file lengths so I don’t know if just grabbing the specific files would help for the filter

 # Setting Folder Redirection Variables
$remotepath = [Environment]::GetFolderPath('desktop').Replace("\Desktop","")