Solved

PowerShell

Posted on 2014-11-17
6
153 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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 …
This article will help you understand what HashTables are and how to use them in PowerShell.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

863 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now