I have problems with deleting my node_modules and vendors folders.
I want to delete them all from computer, I found various of ways on internet and nothing helped actually.
This actually does delete them:
FOR /d /r . %d in (node_modules) DO #IF EXIST "%d" del -f "%d"
but it's asking me every time to type Y in cmd.
Is there any way of doing it with one command in cmd or git bash or powershell? I'm using windows 10.
You can do the following in PowerShell:
Get-ChildItem -Path . -Recurse -Directory -Filter 'node_modules' |
Remove-Item -Recurse -Confirm:$false -WhatIf
Just remove the -WhatIf parameter to do the actual deletion
If you want to target multiple folders recursively, you can do the following:
# Example 1: Using variable for readability
$folders = 'node_modules','vendors'
Get-ChildItem -Path . -Recurse -Directory -Include $folders |
Remove-Item -Recurse -Confirm:$false -WhatIf
# Example 2: Not using variable
Get-ChildItem -Path . -Recurse -Directory -Include 'node_modules','vendors' |
Remove-Item -Recurse -Confirm:$false -WhatIf
I have heard that there could be a performance issue with using -Recurse and -Include together. I have never seen it myself, but keep that in mind if you have a large directory structure and see a performance degradation.
Related
Found a couple of semi-related links:
How to merge / 'flatten' a folder structure using PowerShell - recursive
but my ask is that I have a root dir P:/files
which has several layers of sub directories etc I'd like to flatten all of them so that all the -Files are moved just to the root of P:/files
I don't need to be concerned with duplicates as I'll make sure there are non well before this stage.
it looks like I can use powershell to get a list of all the files no matter the level, and then just for-each over them and a move?
Get-ChildItem -LiteralPath P:\files -Directory | Get-ChildItem -Recurse -File
help on the loop?
A single recursive Get-ChildItem that pipes to Move-Item should do:
Get-ChildItem -File -Recurse -LiteralPath P:\files |
Move-Item -Destination $yourDestination -WhatIf
Note: The -WhatIf common parameter in the command above previews the operation. Remove -WhatIf once you're sure the operation will do what you want.
If you need to exclude files directly located in P:\files:
Get-ChildItem -Directory -Recurse -LiteralPath P:\files | Get-ChildItem -File |
Move-Item -Destination $yourDestination -WhatIf
Note the use of -Directory and -Recurse first, so that all subdirectories in the entire subtree are returned, followed by a non-recursive Get-ChildItem -File call that gets each subdirectory's immediate files only.
I am interested in finding all the directories in the C:\Users directory. I know running "Get-ChildItem C:\Users" display all the directories under the C:\Users directory.
I am trying to write a script that would delete all the directories in C:\Users except for the admin's directory. Running Remove-Item C:\Users* will probably delete all the directories and that's not what I want.
Use
Get-Help Get-ChildItem -online
You'll find the latest and full parameter infomation.in the article, you'll find -Execlude and -Name parameter.
Then use the pipeline mark combine with the remove-item cmdlet, you'll achieve your goal, below just a safe example
# This is a safe command, this will get subdirectories without -Recurse parameter
Get-ChildItem -Exclude Admin -Name | Get-ChildItem
# This is danger
Get-ChildItem -Exclude Admin -Name | Remove-Item -Force
As Mentioned Above do not Remove Public, Default, All users
So an example would be:
get-childitem -Path "C:\users" -Exclude "Administrator","Public","Default","All Users","Default User" -Force -Directory| Remove-Item -Force -Recurse
The -exclude tells it to not look for the foldernames you dont want it to
The -force says to look for hidden files and -directory says to look only at folders
Then Remove-Item -Force -Recurse the -recurse is because the directory is not empty.
I am searching for a file in all the folders.
Copyforbuild.bat is available in many places, and I would like to search recursively.
$File = "V:\Myfolder\**\*.CopyForbuild.bat"
How can I do it in PowerShell?
Use the Get-ChildItem cmdlet with the -Recurse switch:
Get-ChildItem -Path V:\Myfolder -Filter CopyForbuild.bat -Recurse -ErrorAction SilentlyContinue -Force
I use this to find files and then have PowerShell display the entire path of the results:
dir -Path C:\FolderName -Filter FileName.fileExtension -Recurse | %{$_.FullName}
You can always use the wildcard * in the FolderName and/or FileName.fileExtension. For example:
dir -Path C:\Folder* -Filter File*.file* -Recurse | %{$_.FullName}
The above example will search any folder in the C:\ drive beginning with the word Folder. So if you have a folder named FolderFoo and FolderBar PowerShell will show results from both of those folders.
The same goes for the file name and file extension. If you want to search for a file with a certain extension, but don't know the name of the file, you can use:
dir -Path C:\FolderName -Filter *.fileExtension -Recurse | %{$_.FullName}
Or vice versa:
dir -Path C:\FolderName -Filter FileName.* -Recurse | %{$_.FullName}
When searching folders where you might get an error based on security (e.g. C:\Users), use the following command:
Get-ChildItem -Path V:\Myfolder -Filter CopyForbuild.bat -Recurse -ErrorAction SilentlyContinue -Force
Here is the method that I finally came up with after struggling:
Get-ChildItem -Recurse -Path path/with/wildc*rds/ -Include file.*
To make the output cleaner (only path), use:
(Get-ChildItem -Recurse -Path path/with/wildc*rds/ -Include file.*).fullname
To get only the first result, use:
(Get-ChildItem -Recurse -Path path/with/wildc*rds/ -Include file.*).fullname | Select -First 1
Now for the important stuff:
To search only for files/directories do not use -File or -Directory (see below why). Instead use this for files:
Get-ChildItem -Recurse -Path ./path*/ -Include name* | where {$_.PSIsContainer -eq $false}
and remove the -eq $false for directories. Do not leave a trailing wildcard like bin/*.
Why not use the built in switches? They are terrible and remove features randomly. For example, in order to use -Include with a file, you must end the path with a wildcard. However, this disables the -Recurse switch without telling you:
Get-ChildItem -File -Recurse -Path ./bin/* -Include *.lib
You'd think that would give you all *.libs in all subdirectories, but it only will search top level of bin.
In order to search for directories, you can use -Directory, but then you must remove the trailing wildcard. For whatever reason, this will not deactivate -Recurse. It is for these reasons that I recommend not using the builtin flags.
You can shorten this command considerably:
Get-ChildItem -Recurse -Path ./path*/ -Include name* | where {$_.PSIsContainer -eq $false}
becomes
gci './path*/' -s -Include 'name*' | where {$_.PSIsContainer -eq $false}
Get-ChildItem is aliased to gci
-Path is default to position 0, so you can just make first argument path
-Recurse is aliased to -s
-Include does not have a shorthand
Use single quotes for spaces in names/paths, so that you can surround the whole command with double quotes and use it in Command Prompt. Doing it the other way around (surround with single quotes) causes errors
Get-ChildItem V:\MyFolder -name -recurse *.CopyForbuild.bat
Will also work
Try this:
Get-ChildItem -Path V:\Myfolder -Filter CopyForbuild.bat -Recurse | Where-Object { $_.Attributes -ne "Directory"}
Filter using wildcards:
Get-ChildItem -Filter CopyForBuild* -Include *.bat,*.cmd -Exclude *.old.cmd,*.old.bat -Recurse
Filtering using a regular expression:
Get-ChildItem -Path "V:\Myfolder" -Recurse
| Where-Object { $_.Name -match '\ACopyForBuild\.[(bat)|(cmd)]\Z' }
To add to #user3303020 answer and output the search results into a file, you can run
Get-ChildItem V:\MyFolder -name -recurse *.CopyForbuild.bat > path_to_results_filename.txt
It may be easier to search for the correct file that way.
On a Windows system:
Search for all .py files in the 'c:\temp' dir and subdirs, type: dir -r *.py or dir *.py -r
On a *Nix (Linux / MacOs system:
at the terminal type: find /temp -name *.py
This works fine for me.
Generally, robocopy is the fastest and simplest way for searching multiple files in parallel threads. It needs a quite good Powersell code with parallelism to beat that. Here is a link to an article I have written in the past with all the different options you have: Fastest way to find a full path of a given file via Powershell? Check the accepted answer for the best code.
I'm trying to delete all of the desktop.ini files in a given directory; namely Documents\Gio. I've tried del /S desktop.ini and del /S *.ini in cmd (admin mode) and get-childitem .\ -include desktop.ini -recurse | foreach ($_) {remove-item $_.fullname} and get-childitem .\ -include *.ini -recurse | foreach ($_) {remove-item $_.fullname} in PowerShell (also admin). Neither have worked. What should I do?
The desktop.ini files contain the following:
[.ShellClassInfo]
InfoTip=This folder is shared online.
IconFile=C:\Program Files\Google\Drive\googledrivesync.exe
IconIndex=12
I move the directory from my Google Drive folder but all the folders still have the shared icons on them. I was trying to change the directory and all it subdirectories and files' ownership to a different account. I tried to do this with Google Drive but only the root directory changed ownership; I can't delete any of the files or directories therein.
del /s /a desktop.ini
See del /? for help.
I had a similar problem and here is my solution:
Get-Location | Get-ChildItem -Force -Recurse -File -Filter "desktop.ini" | Remove-Item
The first part gets the current active directory.
Get-Location
You could replace it with a path like:
"C:\Users\Chris" | Get-ChildItem -Force -Recurse -File -Filter "desktop.ini" | Remove-Item
The second part gets child items in the path.
Get-ChildItem -Force -Recurse -File -Filter "desktop.ini"
-Force -> force seeing all child items even hidden ones, most "desktop.ini" are hidden
-Recurse -> to be recursive
-File -> to get only files else it could find a folder named "desktop.ini"
-Filter "desktop.ini" -> to only get items named "desktop.ini"
The last part removes the item.
Remove-Item
Adding a -WhatIf for the first run may be safer.
Remove-Item -WhatIf
This is what I used for Windows 2012 server
Create Desktop.ini files
My desktop.ini files were created from running this script which sets default folder options
$key = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced'
Set-ItemProperty $key Hidden 1
Set-ItemProperty $key HideFileExt 0
Set-ItemProperty $key ShowSuperHidden 1
Stop-Process -processname explorer
Remove Desktop.ini files
# Remove from your user desktop
gci "$env:USERPROFILE\Desktop" -filter desktop.ini -force | foreach ($_) {remove-item $_.fullname -force}
# Remove from default desktop
gci "C:\Users\Public\Desktop" -filter desktop.ini -force | foreach ($_) {remove-item $_.fullname -force}
This will grab every folder in your home directory, and then search that folder for desktop.ini and delete it. It should be faster than using -recurse because it won't search subfolders. It should finish neatly with no errors
foreach ($file in Get-ChildItem -path \\server\homedrivedirectory) {
Get-ChildItem $file.FullName -Filter desktop.ini -force | remove-item -force -WhatIf
}
This will grab every folder in your home directory, and then create a path for every folder to \server\homedrivedirectory\user\desktop.ini and then delete that file. It should run a little faster as it doesn't have to search each user folder but it will turn up errors for every user folder that doesn't have a desktop.ini
foreach ($_ in Get-ChildItem -path \\server\homedrivedirectory) {
$path = $_.fullname
Remove-Item "$path\desktop.ini" -Force -WhatIf
}
I've left a -whatif so you can see what it would do in your environment without it doing it. If you want it to actually delete the files remove the -whatif
With powershell, you use the Get-ChildItem (alias gci) cmdlet to retrieve all desktop.ini files and pipe it to Remove-Item (alias rm):
gci 'C:\YOURPATHTODOCUMENTS\GO' -Filter desktop.ini -Recurse | rm
I am new to powershell. I use the following powershell script to copy file from a network share, but the time cost is ridiculously long compared to a traditional windows batch file. What could be the cause?
$dlls=get-childitem -path "\\myShare\myBinFolder" -include *.dll -recurse
copy-item $dlls -destination c:\bins
Thanks
Update - 1 - 1:38 PM 1/13/2011
Why Get-ChildItem is So Slow?
http://blogs.msdn.com/b/powershell/archive/2009/11/04/why-is-get-childitem-so-slow.aspx
Do not use the Include parameter. Use the Filter parameter instead. Include will require every file to be returned from the share and filtered locally. Using Filter should allow the filtering to happen on the remote end.
$dlls = Get-ChildItem -Path "\\myShare\myBinFolder" -Filter *.dll -recurse
or using positional feature of these parameters:
$dlls = Get-ChildItem \\myShare\myBinFolder *.dll -r
In fact, the only time I would ever use Include over Filter is if I needed to specify multiple filter terms (Include takes a string array) e.g.:
Get-ChildItem . -inc *.h,*.cpp,*.rc -r
One way to optimize this is to avoid assigning it to a variable. Try this
Get-ChildItem *.dll -Path \\Myshare\Folder -recurse | % { Copy-item $_.FullName -destination C:\bins }
You can use Measure-Command to measure how much time these two methods are taking. You can do that by:
(Measure-Command { Get-ChildItem *.dll -Path \\Myshare\Folder -recurse | % { Copy-item $_.FullName -destination C:\bins } }).Milliseconds
and
(Measure-Command {$dlls = Get-ChildItem *.dll -Path \\Myshare\Folder -recurse; copy-item $dlls -Destination C:\bins}).Milliseconds
All you really need from the remote system is a list of the full paths to the .dll files in that share. Get-childitem is overkill for that, and has known issues working with large directory structures remotely.
See if this isn't a lot quicker:
cmd /c dir \\Myshare\Folder\*.dll /b /s |% {Copy-Item $_ -destination C:\bins}
Note: the double backslash in the UNC is showing up as a single in the post.
How do I fix that?