Powershell - Access Denied error

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.

May 29th, 2015 1:51am

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.

Free Windows Admin Tool Kit Click here and download it now
May 29th, 2015 2:31am

You can also do this to see what is happening:

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

      

May 29th, 2015 2:33am

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:)

Free Windows Admin Tool Kit Click here and download it now
May 29th, 2015 3:29am

Use RoboCopy.

Type: roboocoopy ./? and follow the instructions.

May 29th, 2015 4:33am

Thank You! I'll try it.
Free Windows Admin Tool Kit Click here and download it now
May 29th, 2015 5:23am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics