Search code examples
powershellpowershell-2.0prtg

Powershell - NewItemUnauthorizedAccessError


I receive the following error when using the New-Item cmdlet to create backup copies of files:

new-item : Access to the path 'C:\Program Files (x86)\PRTG Network Monitor\webroot\mailtemplates' is denied. At line:1 char:21 + foreach ($i in $a) {new-item -itemtype file -name $i.bak} + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : PermissionDenied: (C:\Program File...t\mailtemplates:String) [New-Item], UnauthorizedAcc essException + FullyQualifiedErrorId : NewItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.NewItemCommand

Here is the command I am executing:

pwd
C:\Program Files (x86)\PRTG Network Monitor\webroot\mailtemplates
$a = ls
foreach ($i in $a) {new-item -itemtype file -name $i.bak}

  • I am logged on as the local Administrator
  • Administrator is member of Administrators Group
  • Administrators Group has full permissions on that folder
  • I started the POSH console as Administrator
  • Execution Policy is set to remote signed (just in case it matters)
  • I can use the New-Item cmdlet to create a test.txt file within that folder
  • I can use the GUI to create copies of all files within that directory

Not sure where to go from here.


Solution

  • try this:

    foreach ($i in $a) {new-item  -itemtype file -name "$($i.basename).bak"}