Solved

PowerShell

Posted on 2014-11-17
6
147 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 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

13 Experts available now in Live!

Get 1:1 Help Now