Category Archives: PowerShell

PowerShell – Delete all directories with a given name recursively

 

It turns out to be a bit of a pain to delete all the directories/folders with a certain name.  This’ll do it.

Get-ChildItem C:\path\to\start\directory -Include search1,search2 -Recurse -force | Remove-Item -Recurse -Force

It just gets all the child items in a directory recursively and then removes them.  The -Recurse parameter on the Remove-Item is so that the entire directory and its contents are removed.  search1 and search2 are the the names it is looking for – more or less could be included.

Update last modified date to current date for all items in a directory using powershell

The title says it all.  It’s incredibly simple:

gci | %{$_.LastWriteTime = date}

gci is shorthand for Get-ChildItem, and % is shorthand for ForEach-Object.  $_ refers to the current element being iterated over.  Date returns the current date (and time).

The script therefore gets all items (in the context of the current directory) and updates the LastWriteTime for them all to the current date.  Sorted.