How can I count all files in a specific folder (and all subfolders) with the Powershell command Get-ChildItem?
With (Get-ChildItem <Folder> -recurse).Count also the folders are counted and this is not that what I want. Are there other possibilities for counting files in very big folders quickly?
Does anybody know a short and good tutorial regarding the Windows Powerhell?
I would pipe the result to the Measure-Object cmdlet. Using (...).Count can yield nothing in case there are no objects that match your criteria.
$files = Get-ChildItem <Folder> -Recurse | Where-Object {!$_.PSIsContainer} | Measure-Object
$files.Count
In PowerShell v3 we can do the following to get files only:
Get-ChildItem <Folder> -File -Recurse
Filter for files before counting:
(Get-ChildItem <Folder> -recurse | where-object {-not ($_.PSIsContainer)}).Count
Related
I have a long list of folders. Most of the folders follow the "name_#name" format. I have some that don't follow that structure. I want to move all the folders (and the sub-folders/files within) that DON'T have "_" in the folder name.
For example:
test_#12352
moose_#4532
horse_#84462
cow24
fish3
Moved:
cow24
fish3
I think Move files when they contain a specific word? could be modified to make it work...just not sure how. I'm used to just using GUI, this is my first time using PowerShell
When I tried using that code in that link it didn't work with my situation
What you want to do is just filter the list before you move any files
So you can use the following to pick up all the files you want
$Files = Get-childItem -Path $Path -File
You can then filter it down. My favourite way is to pipe the variable into Where-Object and play around with the individual properties and match types. Since you don't want to include the _ we can use a -notmatch "_" to exclude those values
$Files = Get-ChildItem -Path $Path -File | Where-object{$_.Name -notmatch "_"}
And finally, you can move the files
$Files | move-item -path $_.FullName -Destination $Destination
Or as a one liner
Get-ChildItem -Path $Path -File | Where-object{$_.Name -notmatch "_"} | move-item -path $_.FullName -Destination $Destination
*Please note I haven't really tested this code. So test it out yourself before you run it
I am trying to make a script in powershell to delete all folders in C:\Temp which contains a *.sr_processed file.
I already have this but this only deletes the file and not the folder it was in.
Get-ChildItem -Path C:\Temp -Include *.sr_processed -File -Recurse | foreach { $_.Delete()}
Your are telling it to delete the file. To delete the folder do something like:
Get-ChildItem -Path C:\Temp -Include *.sr_processed -File -Recurse | foreach { Remove-Item –path $_.Directory.Fullname}
If you have multiple .sr_processed files in a folder it might attempt to delete it more than once. And generally deleting a folder you are globbing is bad practice. So a better idea would be to gather up the folders in a list/hash and delete them at the end.
That would look something like:
# declare array
$foldersToDelete = #()
# fill array with folder names
Get-ChildItem -Path C:\Temp -Include *.sr_processed -File -Recurse | foreach { $foldersToDelete += $_.Directory.Fullname}
# sort and make unique
$foldersToDelete = $foldersToDelete | Sort-Object | Get-Unique
# delete folders
$foldersToDelete | Remove-Item –path $_
This is typed from memory, so you might want to adjust it.
I am trying to figure out a command in the CLI which I know in Linux Ubuntu but I need to apply it in Microsoft.
Moreover, I am trying to find all files within a folder with a specific name. Particularly, I try to find all files which contain the term 'test' in their name.
Here is the command which I know in Linux:
find \home\user\data -name '*test*'
Does anyone know the equivalent in the windows command line?
You will looking for Get-ChildItem
Get-ChildItem C:\Test -Filter "*test*"
In PowerShell you can use Get-ChildItem to find files and folders.
If you want to use regular expressions, you can combine Get-ChildItem with the Select-String or Where-Object cmdlets.
Get-ChildItem -Path C:\ -Recurse | Select-String -pattern "Regex"
Get-ChildItem -Path C:\ -Recurse | Where-Object -FilterScript {$_.name -match "regex"}
You're looking for the "dir" command. To accomplish the same thing as your original search, you would use
dir \home\user\data "*test*"
I want to recursive delete all files which have no extension.
I already have the following command to delete files which have a specific extension:
Remove-Item <PATH> -include *.tmp -force -recurse
Get-ChildItem "yourpath" -file -recurse | where {-not $_.extension} | Remove-Item -WhatIf
Note: remove the -whatif to apply the action. -file will only list files and not folders
using the include in the get-childitem will restrict the scope to just that extension.
Here's what worked for me:
Remove-Item C:\Test\*
I am currently trying to run a script I wrote. It works great, but I need it to also search and remove from the hidden folders as well. It does not seem to have any effect on hidden folders... Here is my script.
Get-ChildItem C:\ -Include saplogon.ini -Recurse | foreach ($_) {Remove-Item $_.fullname}
$src_dir = "\\xxxxxxxxxx\xxxxxxxxxxxx\saplogon\saplogon.ini"
$dst_dir = "C:\Windows"
$file = Get-ChildItem $src_dir
Copy-Item $file -Destination $dst_dir
[System.Environment]::SetEnvironmentVariable('SAP_LOGON_INI', 'C:\Windows\saplogon.ini', 'Machine')
You are missing the the -Force parameter.
The code below is using alias so it won't require horizontal scrolling. Know that gci is Get-ChildItem.
Note that you will only be able to get access if you have permission.
gci c:\ -Include saplogon.ini -Recurse -Force | foreach ($_) {remove-item $_.fullname}
At this point, you probably already took care of the non-hidden files. If you want to run the script again, but only for hidden files (and not non-hidden files), you can do that with the -Hidden flag.
Again, you will only be able to get access if you have the permission.
gci c:\ -Include saplogon.ini -Recurse -Hidden | foreach ($_) {remove-item $_.fullname}