Sitecore PowerShell : Remove-Item not deleting, How to troubleshoot? - powershell

I have simple Sitecore8/PowerShell script where I delete image child item and publish parent item:
$child1 = Get-Item $child1Path
Remove-Item $child1 -Force -Permanently
Publish-Item -Item $parent1 -Recurse -PublishMode SingleItem -Language "en*"
No deletion and no errors while running.
How can I find out why it's not deleting ?

You can use the Remove-Item with the -Path parameter. Or use
$child1 | Remove-Item
Apparently you script don't throw an error but the remove is not working that way.
See the documentation https://doc.sitecorepowershell.com/working-with-items

Related

How can I Completely Uninstalling OneDrive and delete OneDrive related Folders on C:\ Drive?

I'm working on removing bloatware that is preinstalled on a number of computers.
I've been able to create a small script to remove the items that are preinstalled from the Microsoft Store and one that uninstalls Teams completely.
However; I'm having some troubles creating a solid script to uninstall OneDrive completely.
So far I have the below:
#Instructions found on https://www.wintips.org/how-to-disable-uninstall-install-onedrive-in-windows-10-8-7/]
#Modified slightly for simplicity and to kill the OneDrive process before uninstallation of application
#To Kill OneDrive.exe process
taskkill /f /im OneDrive.exe
#To uninstall OneDrive if using 64-bit System:
C:\windows\SysWOW64\OneDriveSetup.exe /uninstall
#To uninstall Onedrive if using a 32-bit system:
C:\windows\System32\OneDriveSetup.exe /uninstall
#Added to Removes the OneDrive Folders that are on the laptop.
$dirpath = "C:\Users\$env:UserName\OneDrive"
$dirpath2 = "C:\Users\$env:UserName\OneDrive - CompanyName"
#conditional to delete OneDrive related folders of C Drive. This is where I run into trouble
if ((test-path -LiteralPath $dirpath) -or (test-path -LiteralPath $dirpath2)) {(remove-Item -LiteralPath $dirpath) -or (remove-Item -LiteralPath $dirpath2)}
#Remove-Item -LiteralPath "C:\Users\$env:UserName\OneDrive" -Force -Recurse
#Remove-Item -LiteralPath "C:\Users\$env:UserName\OneDrive - CompanyName" -Force -Recurse
exit
It seems that there might be a logic issue with my conditional statement. When I run this script it does delete both folders that I'm intending to delete, but it returns "False" instead of "True" as I would expect.
I think what is happening is that it is running the remove-Item -LiteralPath $dirpath portion before it is able to reach the logical operator. I'm under this impression, because if I use the -and operator it will only remove the first Folder "C:\Users\$env:UserName\OneDrive"
Any suggestions to resolve this issue or improve the script overall would be appreciated. Thank you.
You should use a foreach
$dirpaths = "C:\Users\$env:UserName\OneDrive", "C:\Users\$env:UserName\OneDrive - CompanyName"
Foreach ($dirpath in $dirpaths) {
if (test-path -LiteralPath $dirpath) {remove-Item -LiteralPath $dirpath}
}
if ((test-path -LiteralPath $dirpath) -or (test-path -LiteralPath $dirpath2)) {(remove-Item -LiteralPath $dirpath) -or (remove-Item -LiteralPath $dirpath2)}
That logic is broken.
Try this:
if (test-path -LiteralPath $dirpath) {
Remove-Item -LiteralPath $dirpath
}
elseif (test-path -LiteralPath $dirpath2){
remove-Item -LiteralPath $dirpath2
}
else {
Write-Error -Message "We ain't found shit!"
}
Lastly
You don't need to do any of that because Remove-Item does not stop if it cannot find a file/path/directory - it continues.
Remove-Item c:\thisisafakepath\, e:\anotherfakepath\, C:\Powershell\TestCSVs\out.csv -WhatIf -ErrorAction SilentlyContinue
Write-Host "Script continues..."
Outputs:
C:> . 'C:\Powershell\Scripts\trydelete.ps1'
What if: Performing the operation "Remove File" on target "C:\Powershell\TestCSVs\out.csv".
Script continues...

How to delete all temp files using powershell

Anyone knows how to delete all temp files using powershell.
Get-ChildItem $env:TEMP\TEMP | Remove-Item -confirm:$false -force -Recurse
I tired this code but it couldn't work.
Can you suggest me any better way to perform the same.
If you don't want to see any errors, you could use the -ErrorAction switch like this:
Remove-Item -Path $env:TEMP\* -Recurse -Force -ErrorAction SilentlyContinue
To empty TEMP folder and leave the folder in place, you should use this command:
Remove-Item $env:TEMP\* -Recurse
If you don't want to type so much, you can use also shorter version:
rm $env:TEMP\* -r
Just use this:
Remove-Item -Path $env:TEMP -Recurse -Force
Of course you will get access errors if any of the files you are deleting are actually being used by the system.
I'm running PS as LOCAdmin and run following command
PS C:\Windows\system32>$tempfolders = #(“C:\Windows\Temp\*”, “C:\Windows\Prefetch\*”, “C:\Documents and Settings\*\Local Settings\temp\*”, “C:\Users\*\Appdata\Local\Temp\*”)
PS C:\Windows\system32>Remove-Item $tempfolders -force -recurse
works for me :)

Remove-Item : Cannot find path '\\footfs32\Builds\TestBuildTest\foo_3.3.17009.3.zip ' because it does not exist

I am trying to remove all files and folders in a specific network path location (the tfs build drop folder of one of my builds).
In my post-build script I tried the following lines: (with same results)
$Destination = '\\footfs32\Builds\TestBuildTest'
Get-ChildItem -Path $Destination -Recurse | Remove-Item -force -recurse
Remove-Item \\footfs32\Builds\TestBuildTest -Force -Recurse
Remove-Item "\\footfs32\Builds\TestBuildTest\*" -Force -Recurse
Remove-Item -LiteralPath "\\footfs32\Builds\TestBuildTest\*" -Force -Recurse
All included folders are being removed, but when it tries to remove the files in the dir, the following error pops ups:
Remove-Item : Cannot find path
'\abctfs32\Builds\TestBuildTest\foo_3.3.17009.3.zip ' because it does
not exist.'
Why is this error popping up on files only, while all folders are deleted correctly? This doesn't make any sense to me. And how do I fix it?
According to the error info.Very likely the file had been deleted in your build process.
Some events triggered during your build process, and that file had been deleted. When running the powershell script, those files couldn't be found. The Remove-item command Can not delete a file twice.
Please double check your build definition, build process and drop folder.
Please try the following command:
Remove-Item -Path "\\footfs32\Builds\TestBuildTest\*.*" -Force -Recurse

Powershell not copying files and not throwing exception

I am not understanding what is happening.
I am attempting to copy and paste dll's from one directory and another.
gci -path $FromPath -Include ("*.dll", "*.pdp") | ? {$_.Name -match "appMaskA|appMaskB|appMaskC"} | foreach{Copy-item $_.Fullname -destination $ToPath -force}
Now that command works for one function that I have it in, but not for this one...
Now, this command is moving dll's to a different server. Not certain why it isn't working.
And if it isn't working it should throw an exception. I did wrap that command in a try catch by the way? Should I be catching a specific exception?
What does your $ToPath look like? If your code is wrapped in try/catch add -ErrorAction Stop parameter to your copy statement as the default value is to continue so the catch block will never be executed.
gci -path $FromPath -Include ("*.dll", "*.pdp") | ? {$_.Name -match "appMaskA|appMaskB|appMaskC"} | foreach{Copy-item $_.Fullname -destination $ToPath -force -ErrorAction Stop}
Does this need to be Powershell or can you use XCOPY via a BASH/CLI script. Using XCOPY you can access C Drive by doing
SERVER.DOMAIN.LOCAL/c$/path/to/dll
Maybe this works for you:
gci -path $FromPath -Include *.dll,*.pdp | where {$_.Name -match "appMaskA|appMaskB|appMaskC"} | Copy-item -path $_ -destination $ToPath -force
For complicated and/or large copying jobs I would use the program robocopy. Robocopy is part of Windows. Execute this command to verify its location:
Get-Command robocopy|select path

PowerShell - suppress Copy-Item 'Folder already exists' error

When I run a recursive Copy-Item from a folder that has sub folders to a new folder that contains the same sub folders as the original, it throws an error when the subfolders already exist.
How can I suppress this because it is a false negative and can make true failures harder to see?
Example:
Copy-Item "C:\realFolder\*" "C:\realFolder_new" -recurse
Copy-Item : Item with specified name C:\realFolder_new\subFolder already exists.
You could try capturing any errors that happen, and then decide whether you care about it or not:
Copy-Item "C:\realFolder\*" "C:\realFolder_new" -recurse -ErrorVariable capturedErrors -ErrorAction SilentlyContinue
$capturedErrors | foreach-object { if ($_ -notmatch "already exists") { write-error $_ } }
If you add -Force to your command it will overwrite the existing files and you won't see the error.
-Recurse will replace all items within each folder and all subfolders.
Copy-Item "C:\realFolder\*" "C:\realFolder_new" -Recurse -Force
You can set the error handling behavior to ignore using:
Copy-Item "C:\realFolder\*" "C:\realFolder_new" -recurse -ErrorAction SilentlyContinue
However this will also suppress errors you did want to know about!