remove selected directories from C:\Users - powershell

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.

Related

Powershell script to copy folder structure and specific file types

I have the following script to copy the folder structure (including empty folders) and specific file types into another directory. However, the issue is that the script copies all files instead of just the .dat and .py files even though I'm using the -Include switch. How to fix this so that it only copies the desired file types
$sourceDir = "C:\User\001"
$targetDir = "C:\User\002"
Get-ChildItem -Path $sourceDir | Copy-Item -Destination $targetDir -Recurse -Include '*.dat', '*.py' -Container
As #Lee_Dailey pointed out, it's probably best to use robocopy for this:
robocopy $sourceDir $targetDir *.dat *.py /e
Yes, this is tricky. You should look up the documentation for the -Include parameter
The Include parameter is effective only when the command includes the contents of an item, such as C:\Windows*, where the wildcard character specifies the contents of the C:\Windows directory.
You could make it work like this:
Copy-Item $sourceDir\* -Destination $targetDir -Recurse -Include '*.dat', '*.py'
-Container is true by default, so you can safely omit it.
Note that you can always use the -WhatIf switch to check if you command will actually do what you want.
You could make it work like this:
i use -Filter
Specifies a filter to qualify the Path parameter. The FileSystem
provider is the only installed PowerShell provider that supports the
use of filters. You can find the syntax for the FileSystem filter
language in about_Wildcards. Filters are more efficient than other
parameters, because the provider applies them when the cmdlet gets the
objects rather than having PowerShell filter the objects after they're
retrieved.
#('*.dat', '*.py') | %{Copy-Item -Path $sourceDir -Destination $targetDir -Recurse -Filter $_ -Force}
or
Copy-Item -Path $sourceDir -Destination $targetDir -Recurse -Filter '*.dat' -Force
Copy-Item -Path $sourceDir -Destination $targetDir -Recurse -Filter '*.py' -Force
it should work but
The Include parameter is effective only when the command includes the
contents of an item, such as C:\Windows*, where the wildcard character
specifies the contents of the C:\Windows directory.
Copy-Item -Destination $targetDir -Recurse -Include '*.dat', '*.py'
may find it easier to include files that can be excluded
Copy-Item -Destination $targetDir -Recurse -Exclude'*.da1', '*.xxx

Can you write in a filter to exclude certain folders when running a Get-ChildItem to compile a list of folder sizes?

I am needing to get a csv file of a group of subfolders within a directory that labels each folder and it's size (preferibly in GB's).
So far, I have this:
$targetPath = '\\folderpath\users'
(Get-ChildItem $targetPath -Recurse | Measure-Object Length -Sum).Sum
There is a folder that resides within each subfolder in the directory, that I do not have permissions/access to so any time I run the script, I get an access denied error.
The folder path looks like this:
'\\folderPath\users\.V6'
Is it possible to write in an exclusion for the above folder and have the script skip it when gathering the size of each of the folders within the Users file? The size of the folder is tiny so it will not have an impact on the results.
Get-ChildItem has a -Include and -Exclude param.
-Exclude <String[]> Specifies, as a string array, an item or items that this cmdlet excludes in the operation. The value of this parameter qualifies the Path parameter. Enter a path element or pattern, such as *.txt. Wildcards are permitted.
# Get specifics for a module, cmdlet, or function
(Get-Command -Name Get-ChildItem).Parameters
(Get-Command -Name Get-ChildItem).Parameters.Keys
Get-help -Name Get-ChildItem -Examples
# Results
<#
Get-ChildItem
Get-Childitem -System -File -Recurse
Get-ChildItem -Attributes !Directory,!Directory+Hidden
dir -att !d,!d+h
dir -ad
Get-ChildItem -File -Attributes !ReadOnly -path C:\ps-test
get-childitem . -include *.txt -recurse -force
get-childitem c:\windows\logs\* -include *.txt -exclude A*
get-childitem -name
#>
Get-help -Name Get-ChildItem -Full
Get-help -Name Get-ChildItem -Online

Windows delete all vendors and node_modules

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.

Using Remove-Item cmdlet but excluding sub-directory

I want to remove the following files from the source, however in the source there is a sub-directory that contains files with similar names. When I run the following command it is deleting files in the sub-directory with similar file name. Is there a way to just delete the files from the source and not the sub-directory?
Example: test_1_file, test_2_file, test_3_file exists in each directory, TestFolder and TestFolder/sub
$source = testfolder
remove-item -Path $source -filter test_*_file -recurse -force
It's usually easiest to pipe the output of Get-ChildItem cmdlet into Remove-Item. You then can use the better filtering of Get-ChildItem as I think -Recurse in Remove-Item has some issues. You can even use Where-Object to further filter before passing to Remove-Item
$source = testfolder
Get-ChildItem -Path $source -Filter test_*_file -Recurse |
Where-Object {$_.Fullname -notlike "$source\sub\*"} |
Remove-Item -Force
If the files to delete:
are all located directly in $source
and no other files / directories must be deleted:
Remove-Item -Path $source/test_*_file -Force
No need for -Recurse (as #Bill_Stewart notes).
Note: For conceptual clarity I've appended the wildcard pattern (test_*_file) directly to the $source path.
Using a wildcard expression separately with -Filter is generally faster (probably won't matter here), but it has its quirks and pitfalls.

Recursively remove desktop.ini files

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