Powershell - Access Denied error RRS feed

  • Question

  • Hi everybody, I'm new here (and in Powershell) and probably this question was asked many times. I just couldn't catch whats wrong with my code (it's simple code to copy folders tree to new tree like year/moth/day/copied_tree). So here is it:

    $path = Get-ChildItem c:\zPS\test\ztest -Recurse | ?{$_.PsIsContainer -eq $true}
    $Year = Get-Date -uFormat "%Y"
    $Month = Get-Date -uFormat "%m"
    $Day = Get-Date -uFormat "%d"
    $dest = $z
    $zpath = 'C:\zPS\test\ztest1\'
    $parent = $path[0].Parent.Name
    $path | foreach {
    $z = New-Item -Path $zpath\"$Year"\"$Month"\"$Day" -Type "directory" -ErrorAction SilentlyContinue
    $_.FullName -match "$parent.+" 
    New-Item -ItemType directory ($dest + $Matches[0]) #-ErrorAction SilentlyContinue

    After I run it, I get following:

    New-Item : Access to the path 'test' is denied.
    At line:11 char:9
    + New-Item <<<<  -ItemType directory ($dest + $Matches[0]) #-ErrorAction SilentlyContinue
        + CategoryInfo          : PermissionDenied: (C:\Program File...2008\ztest\test:String) [New-Item], UnauthorizedAccessException
        + FullyQualifiedErrorId : CreateDirectoryUnauthorizedAccessError,Microsoft.PowerShell.Commands.NewItemCommand

    So hope for you help. Tnx in advance.

    • Moved by Bill_Stewart Wednesday, July 22, 2015 3:16 PM Unanswerable drive-by question
    Friday, May 29, 2015 5:48 AM

All replies

  • New/old, Short/tall fat/small.  One size never fits all.

    This makes no sense:

     New-Item -Path $zpath\"$Year"\"$Month"\"$Day" -Type "directory" -ErrorAction SilentlyContinue

    You are quoting everything to pieces and not looking at the errors.

    Try this:

    $targetPath='C:\zPS\test\ztest1\{0:yyyy}\{0:MM}\{0:dd}' -f [datetime]::Today
    New-Item -Path $targetPath -Type directory | Out-Null

    You cannot keep creating the same folder inside of the loop.

    This error is very explicit.  You have no access to this path: "C:\Program File...2008\ztest\test"

    You should not be copying blindly into the "Programs" folders.  Why would you do this?  It is not a data or user folder set.

    You need to rethink what you are doing as the code makes little sense.  State clearly what you need to do without referencing how.


    Friday, May 29, 2015 6:28 AM
  • You can also do this to see what is happening:

    $path |
        foreach {
            $_.FullName -match "$parent.+"
            Write-Host ($dest + $Matches[0]) -fore green



    Friday, May 29, 2015 6:30 AM
  • Tnx for your reply!

    Actually I have access to used path (it's local folder and used to test my scripts). I'm just started with PowerShell.

    What I want from my scripts:

    It'll work non stop(endless loop) "waiting" files to be generated by another script (or one day when I get better in PowerShell it'll be one script) and move the files to the "copied" folder tree with addition like I described before (year/moth/day/copied_tree/xx/xx/xx/moved_file), something like backup. At the moment I've 2 separated scripts, one for moving files if they are appropriate, second is "this"(folder tree copier). I'll try to combine them soon:)

    As I understood you, I shouldn't put year/month/date into loop, right? But I need this part to be updated every day(and later may be hour).

    So could you give some more advices for my task (it'd be very kind of you:))

    P.S. Sorry if i had grammar mistakes, I'm not native English. Tnx for understanding:)

    Friday, May 29, 2015 7:29 AM
  • Use RoboCopy.

    Type: roboocoopy ./? and follow the instructions.


    Friday, May 29, 2015 8:32 AM
  • Thank You! I'll try it.
    Friday, May 29, 2015 9:22 AM