Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, November 9, 2011 9:20 PM
I have scripts that prune operating logs by date, but in this case I need to delete only one file among a group. It doesn't matter which one, just any one of the files. To paint the picture, we use files on a shared volume to artifically fill up the volume. The space is presented by our SAN and when our users run out of space we simply remove one of these large files to stop the denial of service due to disk space. I want to create a powershell script that will simply remove "one" of the files to elleviate the disk space issue.
Any thoughts? Thanks in advance!
All replies (7)
Wednesday, November 9, 2011 10:16 PM ✅Answered | 1 vote
Try this:
Get-ChildItem M:\ | Sort CreationTime | Select -First 1 | Remove-Item
It gets a list of all items, sorts them by creation date, grabs the last one in the list (oldest) and then deletes the item.
Rich Prescott | Infrastructure Architect, Windows Engineer and PowerShell blogger | MCITP, MCTS, MCP
Engineering Efficiency
@Rich_Prescott
Client System Administration tool
AD User Creation tool
Wednesday, November 9, 2011 11:40 PM ✅Answered | 1 vote
Try this:
Get-ChildItem M:\ | Sort CreationTime | Select -Last 1 | Remove-Item
It gets a list of all items, sorts them by creation date, grabs the last one in the list (oldest) and then deletes the item.
You want to use select -first 1 rather than last.
Thursday, November 10, 2011 2:40 AM
You want to use select -first 1 rather than last.
That's what I get for not testing. Thanks for the correction, post edited.
Rich Prescott | Infrastructure Architect, Windows Engineer and PowerShell blogger | MCITP, MCTS, MCP
Engineering Efficiency
@Rich_Prescott
Client System Administration tool
AD User Creation tool
Thursday, November 10, 2011 6:24 PM
Fellas! Easier than I thought.... Thanks again for the help it works like a charm.
Thursday, May 25, 2017 12:26 PM
Just to let you know I was in a rush, copy-pasted your command line and this deleted thousands of files on my M: drive in a few seconds. Now I have been running three different file restore softwares for the last 72 hours. I am not saying this is your fault, but a warning in your post would be nice...
Thursday, May 25, 2017 6:02 PM | 1 vote
Just to let you know I was in a rush, copy-pasted your command line and this deleted thousands of files on my M: drive in a few seconds. Now I have been running three different file restore softwares for the last 72 hours. I am not saying this is your fault, but a warning in your post would be nice...
You clearly copied the script incorrectly.
Never run any script that you do not completely understand. This is your responsibility. The script posted cannot do what you claim.
\(ツ)_/
Thursday, May 25, 2017 8:34 PM
Just to let you know I was in a rush, copy-pasted your command line and this deleted thousands of files on my M: drive in a few seconds. Now I have been running three different file restore softwares for the last 72 hours. I am not saying this is your fault, but a warning in your post would be nice...
You clearly copied the script incorrectly.
Never run any script that you do not completely understand. This is your responsibility. The script posted cannot do what you claim.
\(ツ)_/
JRV is completely right!
If you copy/paste the command as it is, it only deletes one single item (file or folder).
If this item is a folder, by default Powershell denies to remove it, and you must add more parameters (-recurse -force).
Thus, the command line provided could not have "deleted thousands of files" without modifying it.