?
Solved

PowerShell

Posted on 2014-11-17
6
Medium Priority
?
168 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 16

Assisted Solution

by:Joshua Grantom
Joshua Grantom earned 2000 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
Is Your AD Toolbox Looking More Like a Toybox?

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.

 
LVL 16

Assisted Solution

by:Joshua Grantom
Joshua Grantom earned 2000 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 2000 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 AD Toolbox Looking More Like a Toybox?

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the adminiā€¦
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

764 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