Solved

PowerShell

Posted on 2014-11-17
6
157 Views
1 Endorsement
Last Modified: 2014-12-15
Good morning,

I have about 90 files in a temp directory on about 1000 machines named macro.x02 - macro.x99, possible you can give me a example of a powershell script that will delete all these files if found?    

Thanks
1
Comment
Question by:SDJ_1
  • 3
  • 3
6 Comments
 
LVL 16

Assisted Solution

by:Joshua Grantom
Joshua Grantom earned 500 total points
ID: 40447318
Here you go, just change the MyTempdirectory to the folder the temp files reside. Here are 2 options, by list or by OU. If you have them sorted another way, let me know.

I kept "-whatif" on there so you can see what files it chooses. Once you are sure it is working how you want, remove "-whatif"

By a list of computers
$computers = Get-Content C:\ComputerList.txt | foreach {
Get-ChildItem "\$_\c$\MyTempDirectory" -include "macro.x*" -recurse | foreach ($_) {remove-item $_.fullname -whatif}
}

Open in new window


By OU
$computers = Get-ADComputer -| foreach {
Get-ChildItem "\$_\c$\MyTempDirectory" -include "macro.x*" -recurse | foreach ($_) {remove-item $_.fullname -whatif}
}

Open in new window

0
 

Author Comment

by:SDJ_1
ID: 40447671
Thanks for responding, I tried both versions and its failing.    I'm using Powershell version 2.    The script will be copied to all the affected machines and will be executed from a temp directory.
0
 

Author Comment

by:SDJ_1
ID: 40447680
I used the second option:

$computers = Get-ADComputer -| foreach {
Get-ChildItem "\$_\d$\Prgram Files\POSWIN\Parm" -include "macro.x*" -recurse | foreach ($_) {remove-item $_.fullname -whatif}
}


Getting the following error:

Get-ChildItem : Cannot find path '\\d$\Program files\POSWIN\Parm' because it does not exist.
At C:\Documents and Settings\Cambridge\Desktop\MacroDelete2.ps1:2 char:14
+ Get-ChildItem <<<<  "\$_\d$\Program files\POSWIN\Parm" -include "macro.x*" -recurse | foreach ($_) {remove-item $_.fullname -whatif}
    + CategoryInfo          : ObjectNotFound: (\\d$\Program files\POSWIN\Parm:String) [Get-ChildItem], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 16

Assisted Solution

by:Joshua Grantom
Joshua Grantom earned 500 total points
ID: 40447684
If the script is being copied and run locally then you only need this

This just shows what will happen.
Get-ChildItem "D:\Program Files\POSWIN\Parm" -include "macro.x*" -recurse | foreach ($_) {remove-item $_.fullname -whatif}

Open in new window


This will actually delete them.
Get-ChildItem "D:\Program Files\POSWIN\Parm" -include "macro.x*" -recurse | foreach ($_) {remove-item $_.fullname}

Open in new window

0
 

Author Comment

by:SDJ_1
ID: 40447712
Ok that was perfect only thing I only need it to delete macro.x02 - .x99 only above option is awesome but its deleting everything.
0
 
LVL 16

Accepted Solution

by:
Joshua Grantom earned 500 total points
ID: 40447849
Here you go. Remove "-Whatif" to have it actually remove the files.

$files = 2..99 | % { "macro.x{0:00}" -f $_ }
foreach($file in $files) {
Get-ChildItem "D:\Program Files\POSWIN\Parm" -Include $ext -recurse | foreach {Remove-Item $_.fullname -WhatIf}
}

Open in new window

1

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question