Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

trouble with a powershell script

Closed Thread
 
Thread Tools Display Modes
Old 07-14-2008   #1 (permalink)
bj.daniels.0000
Guest


 

trouble with a powershell script

i have a simple litte script in which i am trying to delete all the
contents of a bunch of directories (not the directories themselves).
it worked fine in my testing - but when i try to run it against the
actual folders, i get an error:

Not Enough permission to perform operation.

i am logged on as an admin and can manually delete the files/folders.
I am assuming that there is some context issues or something. I am
using PowerGUI.

here is the main code

ForEach ($HomeFolder in dir $Basepath)
{`
$delPath = $homefolder.FullName + "\*"
write-host $delPath
Remove-Item $delpath -recurse
}


any thoughts

thanks

bj
Old 07-14-2008   #2 (permalink)
Shay Levy [MVP]
Guest


 

Re: trouble with a powershell script


Seems there are some read-only files in one or more directories, try to add
-force to remove-item.
Also, you'll probably get another error, like:

An object at the specified path <path to file> does not exist.


That's because your dir include files and you are appending "\*" to each
file found.
To fix it, include directories only (e.g. $_.PSIsContainer):

ForEach ($HomeFolder in (dir $Basepath | where {$_.PSIsContainer})){
$delPath = $homefolder.FullName
write-host $delPath
Remove-Item $delpath -recurse
}



---
Shay Levy
Windows PowerShell MVP
blog: http://blogs.microsoft.co.il/blogs/ScriptFanatic


Quote:

> i have a simple litte script in which i am trying to delete all the
> contents of a bunch of directories (not the directories themselves).
> it worked fine in my testing - but when i try to run it against the
> actual folders, i get an error:
>
> Not Enough permission to perform operation.
>
> i am logged on as an admin and can manually delete the files/folders.
> I am assuming that there is some context issues or something. I am
> using PowerGUI.
>
> here is the main code
>
> ForEach ($HomeFolder in dir $Basepath)
> {`
> $delPath = $homefolder.FullName + "\*"
> write-host $delPath
> Remove-Item $delpath -recurse
> }
> any thoughts
>
> thanks
>
> bj
>

Old 07-14-2008   #3 (permalink)
Jon
Guest


 

Re: trouble with a powershell script

<bj.daniels.0000@xxxxxx> wrote in message
news:ba232cee-8296-4322-84db-a1d4b34b135d@xxxxxx
Quote:

>i have a simple litte script in which i am trying to delete all the
> contents of a bunch of directories (not the directories themselves).
> it worked fine in my testing - but when i try to run it against the
> actual folders, i get an error:
>
> Not Enough permission to perform operation.
>
> i am logged on as an admin and can manually delete the files/folders.
> I am assuming that there is some context issues or something. I am
> using PowerGUI.
>
> here is the main code
>
> ForEach ($HomeFolder in dir $Basepath)
> {`
> $delPath = $homefolder.FullName + "\*"
> write-host $delPath
> Remove-Item $delpath -recurse
> }
>
>
> any thoughts
>
> thanks
>
> bj

You'd also need to be careful not to delete subfolders along with the files
(which you will do with a blanket Remove-Item -Recurse .... statement).
Something like this would avoid that by focussing just on files ....

dir $BasePath -Force -Recurse | `
where {Test-Path $_.FullName -PathType Leaf} | `
Remove-Item -Force

--
Jon



Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Powershell script help Tboz PowerShell 3 4 Weeks Ago 08:58 AM
when run powershell script as windows service ,powershell fail powershell fail on winodws 2008 PowerShell 6 01-15-2008 03:20 PM
PowerShell script using PowerSMO Marco Shaw PowerShell 0 09-25-2007 08:06 AM
trouble running a script BJ Daniels PowerShell 3 08-20-2007 03:10 PM
How to schedule a Powershell script?? jim PowerShell 5 10-19-2006 10:15 PM








Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50