2

On our servers we have files similar to the following:

C:\Documents and Settings\username\Application Data\Sun\Java\jre1.6.0_01\
C:\Documents and Settings\username\Application Data\Sun\Java\jre1.6.0_02\
C:\Documents and Settings\username\Application Data\Sun\Java\jre1.6.0_03\
etc

We don't need these files any more so I am trying to write a powershell script to delete them. For now I am limiting myself to one particular server and I have the following script:

 $path = "\\server\c$\Documents and Settings\"
 Get-ChildItem $path -Recurse | Where {$_.PSIsContainer -and ($_ -match '^jre1.6.0_')} | Remove-Item -Recurse -WhatIf

The problem is that the pattern I am using doesn't seem to be calling a match and I'm not sure what I'm doing wrong. I've tried several permutations and none seem to work. Can any regex gurus out there shed some light on what I might be doing wrong?

Thanks Brad

2 Answers 2

3

Try like this:

Get-ChildItem $path -Recurse -Force | ....

-Force looks for hidden or system files/folders also.

The "Application Data" folder is Hidden!

Sign up to request clarification or add additional context in comments.

1 Comment

That was it! It wasn't going into the hidden folders. Thank you!
1

The regex is ok, I think you should try

$_.Name -match '^jre1.6.0_'

and not just

$_ -match '^jre1.6.0_'

10 Comments

Using $_ -match ... relies on the object's ToString() behavior. So yeah, I'd use $_.Name -match ... in a script.
That doesn't seem to be working either. I've let the script run for 10+ minutes and its not returning anything. I'll continue to let it run till it completes to see if maybe it just needs more time to recurse all the directories.
It finally finished - no results. PS C:\Documents and Settings\user\desktop> .\cleanjava.ps1 PS C:\Documents and Settings\user\desktop>
@Brad: maybe you got no results because you are filtering on $_.PSIsContainer, and that gets only directories?
Thats exactly what I am looking for - directories whose name begins with jre1.6.0_
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.