I'm trying to write a PowerShell script which goes through a list of values which are folder or file paths, and delete the files first, then remove the empty folders.
My script so far:
[xml]$XmlDocument = Get-Content -Path h:\List_Files.resp.xml $Files = XmlDocument.OUTPUT.databrowse_BrowseResponse.browseResult.dataResultSet.Path
Now I'm trying to test each line in the variable to see if it's a file and delete it first, and then go through and remove subfolders and folders. This is just so that it's a clean process.
I can't quite get this next bit to work, but I think I need something like:
foreach ($file in $Files) { if (! $_.PSIsContainer) { Remove-Item $_.FullName} } }
The next section can clean up the subfolders and folders.
Any suggestions?
To search the content in the file in PowerShell, you need to first get the content from the file using Get-Content command and then you need to add Select-String pipeline command. In the below example, we need to search the lines which contain the Get word. You can use multiple patterns to search for the result.
List $Env:Path with PowerShell. You can also see path values in the Control Panel; navigate to the System section and then click on the link to 'Advanced system settings'.
-Leaf. An element that does not contain other elements, such as a file. The easiest way to get this information from the console is Get-Help Test-Path -Parameter PathType.
Static member operator :: To find the static properties and methods of an object, use the Static parameter of the Get-Member cmdlet. The member name may be an expression. PowerShell Copy.
I found a workaround for this question: use Test-Path
cmdlet with the parameter -FileType
equal to Leaf
for checking if it is a file or Container
for checking if it is a folder:
# Check if file (works with files with and without extension) Test-Path -Path 'C:\Demo\FileWithExtension.txt' -PathType Leaf Test-Path -Path 'C:\Demo\FileWithoutExtension' -PathType Leaf # Check if folder Test-Path -Path 'C:\Demo' -PathType Container
I originally found the solution here. And the official reference is here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With