2

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.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
Kyra
  • 27
  • 7

1 Answers1

0

try this:

foreach ($i in $a) {new-item  -itemtype file -name "$($i.basename).bak"}
CB.
  • 58,865
  • 9
  • 159
  • 159
  • Here's what I get: `foreach ($i in $a) {new-item -itemtype file -name "$($i.htm).txt"} -a--- 3/26/2014 8:17 AM 0 .txt new-item : The file 'C:\Program Files (x86)\PRTG Network Monitor\webroot\mailtemplates\.txt' already exists. At line:1 char:21 + foreach ($i in $a) {new-item -itemtype file -name "$($i.htm).txt"} + + CategoryInfo : WriteError: (C:\Program File...ltemplates\.txt:String) [New-Item], IOException + FullyQualifiedErrorId : NewItemIOError,Microsoft.PowerShell.Commands.NewItemCommand` – Kyra Mar 26 '14 at 13:24
  • Worked as advertised. Not familiar with the syntax. I misunderstood what you meant by basename. Thank you. – Kyra Mar 26 '14 at 14:24
  • @kira Ok. basename is a property of a [fileinfo] object. the sintax `$($variablename.properties)` is neeeded whan you need to expand in a string the property value of a variable. – CB. Mar 26 '14 at 14:27