powershell -exclude did not exclude all the files from list - powershell

I have this script:
$list= #("inetpub", "Program Files", "Program Files (x86)", "ProgramData", "Windows", "Users"); Get-Item "C:\*" -exclude $list | Remove-Item -Recurse -force
and it should delete everything that is root but nothing from the list and what is inside the folders from the list. When I run it, somehow the script deletes some of Program Files folders like notepad++. How can i fix it?
The script is deployed using GPO like this
-ExecutePolicy Bypass -command "$list= #("inetpub", "Program Files", "Program Files (x86)", "ProgramData", "Windows", "Users"); Get-Item "C:\*" -exclude $list | Remove-Item -Recurse -force"
Edit: If it's deployed as a script not as a command it is working. But i would still like to know how can i make it work as a command

Is that a startup script? This works from cmd at least:
powershell $list = 'inetpub','Program Files','Program Files (x86)','ProgramData','Windows','Users'; Get-Item C:\* -exclude $list ^| Remove-Item -Recurse -force -whatif

Related

remove selected directories from C:\Users

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 want to delete the currently executing folder and its content in poweshell

$Path = Get-Location | Select -expand Path
Set-Location ..
Remove-Item -LiteralPath "FolderLocation" -Recurse -Force
This isn't working when I execute the powershell file through .cmd file.
What about:
$Path = Get-Location
Set-Location ..
Remove-Item -Path $Path -Recurse -Force
This worked for me but I did not have any files or folders in that folder.

Windows PowerShell is in NonInteractive mode. Read and Prompt functionality is not available

I am new to the power shell scripting. I am trying to delete all files except one folder and one file. I run this script by using jenkins it showing error called " Windows PowerShell is in NonInteractive mode. Read and Prompt functionality is not available." And i am trying to run this script in powershell window but it asking Confirmation [Y/N]. I need to run this script by using jenkins please help me.
$Path = "C:\TeamCity\buildAgent2\work"
$exclude = #("*.old", "*directory.map")
Get-ChildItem $Path -Exclude $exclude | Remove-Item -Force -Confirm:$false -ErrorAction Stop| echo Y
You need to add the Recurse parameter to the remove command.
Like that:
Remove-Item -Recurse -Force
guiwhatsthat is correct your code should like this;
$Path = "C:\TeamCity\buildAgent2\work"
$exclude = #("*.old", "*directory.map")
Get-ChildItem $Path -Exclude $exclude | Remove-Item -Recurse -Force -Confirm:$false -ErrorAction Stop

Copy-Item is not working

I have a script that is finding a few files and the copying them. This is part of a psake build script.
The command is:
Get-ChildItem -Path "$sourceFolder" -Include "*.ispac" -Recurse `
| Select-Object -ExpandProperty FullName `
| Copy-Item -Destination "$caseFolder\" -Force -Verbose
When I execute this, I get this for the message:
VERBOSE: Performing the operation "Copy File" on target
"Item: C:\Source\TestSssisOne.ispac
Destination: C:\Destination\TestSssisOne.ispac".
That sure looks like the files where copied. But they aren't. No errors. If I copy this command out to ISE and setup the variables, it copies no problem. I tried to just manually copy a single file, with explicit paths. Again, in script it does not copy, but in PS console or ISE it does.
I have no idea what could be the problem. I've used Copy-Item in psake scripts. In fact, I copied the above code to a later task and it works! In the task where it isn't working I'm calling msbuild to build a solution.
Any insight appreciated!
modify your code like this
Get-ChildItem -Path "$sourceFolder" -Include "*.ispac" -Recurse -File | foreach{Copy-Item $_.FullName -Destination (("$caseFolder\") + $_.Name) -Force -Verbose }

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