locked
rename pst files to owner RRS feed

  • Question

  • Here is the script:

    $pathLocation = "C:\*"
    $filter = ".pst"
    
    $allFiles = Get-ChildItem -Path $pathLocation -Filter "*$filter"
    
    foreach ($f in $allFiles){
       $fileOwner = $(Get-Acl $f).owner
       $renameTo = -join($fileOwner, $filter)
       $f | Rename-Item -NewName $renameTo
    }

    But i get an error from the Rename part. I tried Move-Item but also with no results.

    Error:

    Rename-Item : Cannot rename the specified target, because it represents a path or device name.

    Tuesday, September 19, 2017 2:25 PM

Answers

  • This would be the easiest way to do this:

    Get-ChildItem C:\Users\tester\* -Filter *.pst -recurse |
    	ForEach-Object{
    		$owner = ( Get-Acl $_ ).Owner -replace '\\', '_'
    		$newname = $_.BaseName + "_$owner.pst"
    		$_ | Rename-Item -NewName $newname -Verbose -WhatIf
    	}

    You cannot use C:\* as a starting point or you will get hundreds of errors.


    \_(ツ)_/



    • Edited by jrv Wednesday, September 20, 2017 9:43 AM
    • Marked as answer by cvanaxel Wednesday, September 20, 2017 2:38 PM
    Wednesday, September 20, 2017 9:42 AM
  • What version of PS?  That code can never produce that error without some form of a system mismatch.

    Try changing to this:

     $owner = ( $_ | Get-Acl ).Owner -replace '\\', '_'


    \_(ツ)_/

    • Marked as answer by cvanaxel Wednesday, September 20, 2017 2:38 PM
    Wednesday, September 20, 2017 2:17 PM

All replies

  • $allFiles = Get-ChildItem -Pathc:\users\* -Filter*.pst

    Enumerating from the root will cause all kinds of issues.


    \_(ツ)_/

    Tuesday, September 19, 2017 4:56 PM
  • $allFiles = Get-ChildItem -Pathc:\users\* -Filter*.pst

    Enumerating from the root will cause all kinds of issues.


    \_(ツ)_/

    I changed it but got no error. But the file name did not change. 

    $allFiles = Get-ChildItem -Path "C:\Users\tester\*" -Filter "*.pst"
    
    foreach ($f in $allFiles){
       $fileOwner = $(Get-Acl $f).owner
       $renameTo = -join($fileOwner, '.pst')
       $f | Rename-Item -NewName $renameTo
    }

    Tuesday, September 19, 2017 7:50 PM
  • You are still just guessing.

    $owner = (Get-Acl test.ps1).Owner -replace '\\','_'
    $newname = $f.BaseName + "_$owner.pst"
    $f | Rename-Item -NewName $newname


    \_(ツ)_/


    • Edited by jrv Wednesday, September 20, 2017 8:18 AM
    Tuesday, September 19, 2017 8:05 PM
  • I fixed it. Thank you.

    $allFiles = Get-ChildItem -Path "C:\tester\*" -Filter *.pst -r
    
    foreach ($f in $allFiles){
       $fileOwner = (Get-Acl $f).Owner.Split("\")[1]
       $renameTo = "$fileOwner.pst"
       $f | Rename-Item -NewName $renameTo
    }

    problem was in the get-childitem part. If i want to add a if else statement. If the name already exists add an count up on it.

    example:

    doom.pst exists it needs to create doom1.pst


    Because if you have 2 pst in the same folder it wont rename the other one.
    • Edited by cvanaxel Tuesday, September 19, 2017 10:01 PM
    Tuesday, September 19, 2017 9:13 PM
  • Why is the script only working when i'm in the base folder it self and not from the grandparent folder
    Wednesday, September 20, 2017 8:00 AM
  • Why is the script only working when i'm in the base folder it self and not from the grandparent folder

    Because you chose to ignore my code which prevents many things.


    \_(ツ)_/

    Wednesday, September 20, 2017 8:18 AM
  • The only thing different is the replace. I used Split and the $renameTo
    Wednesday, September 20, 2017 8:29 AM
  • No. You made significant and impacting changes.  Use my code and you will see.


    \_(ツ)_/

    Wednesday, September 20, 2017 8:34 AM
  • But this only will do one pst file. It is a typo the test.ps1?

    $owner = (Get-Acl test.ps1).Owner -replace '\\','_'

    Wednesday, September 20, 2017 9:12 AM
  • But this only will do one pst file. It is a typo the test.ps1?

    $owner = (Get-Acl test.ps1).Owner -replace '\\','_'


    Yes. It is a placeholder where you would put your file/filename.

    \_(ツ)_/

    Wednesday, September 20, 2017 9:19 AM
  • This would be the easiest way to do this:

    Get-ChildItem C:\Users\tester\* -Filter *.pst -recurse |
    	ForEach-Object{
    		$owner = ( Get-Acl $_ ).Owner -replace '\\', '_'
    		$newname = $_.BaseName + "_$owner.pst"
    		$_ | Rename-Item -NewName $newname -Verbose -WhatIf
    	}

    You cannot use C:\* as a starting point or you will get hundreds of errors.


    \_(ツ)_/



    • Edited by jrv Wednesday, September 20, 2017 9:43 AM
    • Marked as answer by cvanaxel Wednesday, September 20, 2017 2:38 PM
    Wednesday, September 20, 2017 9:42 AM
  • Yeah this work but only for a single file. If i put $owner = (Get-Acl $f).Owner -replace '\\','_' then it won't work.
    Wednesday, September 20, 2017 9:43 AM
  • Yeah this work but only for a single file. If i put $owner = (Get-Acl $f).Owner -replace '\\','_' then it won't work.

    What do you mean by "it won't work"?


    \_(ツ)_/

    Wednesday, September 20, 2017 9:45 AM
  • Still an error. the get-acl has a problem with the path.

    Get-Acl : Cannot validate argument on parameter 'Path'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.

    Wednesday, September 20, 2017 9:48 AM
  • Still an error. the get-acl has a problem with the path.

    Get-Acl : Cannot validate argument on parameter 'Path'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.

    What code are you running?  You need to be explicit.  What did you run to get the error?  Don't make us guess.

    \_(ツ)_/

    Wednesday, September 20, 2017 9:50 AM
  • I ran your code.

    Get-ChildItem C:\Users\tester\* -Filter *.pst -recurse |
    	ForEach-Object{
    		$owner = (Get-Acl $_.Owner) -replace '\\', '_'
    		$newname = $_.BaseName + "_$owner.pst"
    		$_ | Rename-Item -NewName $newname -WhatIf
    	}

    Wednesday, September 20, 2017 9:57 AM
  • Please copy it again.  Be sure it copies correctly.  I made an edit immediately after I posted but the edit didn't get to all servers so you got the unedited version.


    \_(ツ)_/


    • Edited by jrv Wednesday, September 20, 2017 10:18 AM
    Wednesday, September 20, 2017 10:17 AM
  • Same error

    Get-Acl : Cannot find path 'blabla_.pst' because it does not exist.

    Wednesday, September 20, 2017 10:27 AM
  • Same error

    Get-Acl : Cannot find path 'blabla_.pst' because it does not exist.

    Post the exact code and the complete error.  We cannot keep guessing.  I cannot see your screen.  There is NO reason for that to happen if you have the correct code copied.


    \_(ツ)_/

    Wednesday, September 20, 2017 10:57 AM
  • Get-ChildItem C:\Users\c.vanaxel\Desktop\tester\* -Filter *.pst -recurse |
    	ForEach-Object{
    		$owner = ( Get-Acl $_ ).Owner -replace '\\', '_'
    		$newname = $_.BaseName + "_$owner.pst"
    		$_ | Rename-Item -NewName $newname -Verbose -WhatIf
    	}

    Same error

    Get-Acl : Cannot find path 'blabla_.pst' because it does not exist.


    • Edited by cvanaxel Wednesday, September 20, 2017 11:23 AM
    Wednesday, September 20, 2017 11:22 AM
  • You are not posting the complete error message.  What you are implying is impossible as posted.  Post the complete and exact error.


    \_(ツ)_/

    Wednesday, September 20, 2017 11:35 AM
  • Get-Acl : Cannot find path 'blabla.pst' because it does not exist.
    At line:3 char:14
    +         $owner = ( Get-Acl $_ ).Owner -replace '\\', '_'
    +                    ~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (:) [Get-Acl], ItemNotFoundException
        + FullyQualifiedErrorId : GetAcl_PathNotFound_Exception,Microsoft.PowerShell.Commands.GetAclCommand
     
    What if: Performing the operation "Rename File" on target "Item: C:\Users\c.vanaxel\Desktop\tester\blabla\E-mailA
    rchief\blabla.pst Destination: C:\Users\c.vanaxel\Desktop\tester\blabla\E-mailArchief\clinton_.pst".

    It works if you run the script the folder where the pst is.

    So if i run the script in: C:\Users\c.vanaxel\Desktop\tester\blabla\E-mailArchief>.\changename.ps1 it then works.

    Wednesday, September 20, 2017 2:10 PM
  • What version of PS?  That code can never produce that error without some form of a system mismatch.

    Try changing to this:

     $owner = ( $_ | Get-Acl ).Owner -replace '\\', '_'


    \_(ツ)_/

    • Marked as answer by cvanaxel Wednesday, September 20, 2017 2:38 PM
    Wednesday, September 20, 2017 2:17 PM
  • That was it. It worked thank you :) but this only works if there is one file in the folder. Am i wright?

    ps version:

    PS C:\> $PSVersionTable.PSVersion
    Major  Minor  Build  Revision
    -----  -----  -----  --------
    5      1      15063  502    


    • Edited by cvanaxel Wednesday, September 20, 2017 2:39 PM
    Wednesday, September 20, 2017 2:38 PM
  • I tested on 5.1 and did not have that issue.  You may be missing a Net update.

    \_(ツ)_/

    Wednesday, September 20, 2017 2:41 PM
  • Is it an easy fix to add a count on it. So if there are two pst in the same folder it rename the first one as blabla01.pst and the second one as blabla02.pst.
    Wednesday, September 20, 2017 2:47 PM
  • I'm trying to do increment on the filename but it change both in the same name.

    I did something like this.

    Get-ChildItem C:\Users\Amsterdam01\Desktop\tester\* -Filter *.pst -recurse |
    	ForEach-Object{
            $nr = 1
    		$owner = ( $_ | Get-Acl ).Owner.Split("\")[1]
    		$newname = ("$owner{0}.pst" -f $nr++)
    		$_ | Rename-Item -NewName $newname -Verbose -WhatIf
    	}

    Wednesday, September 20, 2017 4:32 PM