How to delete folders that aren't empty? - powershell-4.0

I am using these lines. Is there any way to shorten it or is this the best?
Objective is to delete the Refresh folder and all of its contents. The script works, but I'd like it to be as minimal as possible.
# Set var $Path to equal Refresh folder
$Path = "C:\data\Refresh"
# Find sub-folders of Refresh
Get-ChildItem -Path $path -Recurse | Remove-Item -Force -Recurse
# Delete Refresh top level folder itself
Remove-Item $Path -Force

Since you want to delete the folder and all its content don't bother with Get-ChildItem. Simply call Remove-Item with the path you want to delete.
$Path = 'C:\data\Refresh'
Remove-Item $Path -Recurse -Force
or just
Remove-Item 'C:\data\Refresh' -Recurse -Force

Related

How to create a new alias in Windows Powershell for deleting multiple folders?

I want to create an alias in Windows PowerShell to delete multiple folders from the command line.
To remove more than one item I call:
Remove-Item '.\Documents\*\Bin\' ,'.\Documents\*\Inter\' -Force -Recurse
I have tried to create the alias like this:
New-Alias -Name 'Clean-RCD' Remove-Item '.\Documents\*\Bin\' ,'.\Documents\*\Inter\' -Force -Recurse
Output:
New-Alias: A positional parameter cannot be found that accepts argument 'System.Object[]'.
Any idea how to define this alias correctly?
Unlike in bash, aliases in PowerShell are strict 1-to-1 command name mappings - no extra parameter arguments allowed.
You'll want to create a function instead:
function Clean-RCD {
Remove-Item -Path '~\Documents\*\Bin', '~\Documents\*\Inter\' -Force -Recurse
}
Use of ~ (which resolves to your home folder) over . is intentional - this way it'll still work if you've navigated to a different path

Find specific .exe files on a computer with PowerShell

So at the company I am currently doing my internship at, they have about 20 'technical laptops' that have company-specified software. They are located in the C:\ directory.
For example:
aaa.exe is located at C:\aaa\aaa.exe
bbb.exe is located at C:\bbb\bbb.exe
What I actually need is a .ps1 script to show these specific executables in a list.
What I have so far is:
Get-ChildItem C:\ -recurse | where {$_.extension -eq ".exe"}
Now, I believe it's just edit the search query because this gives me all the .exe files on the C:\ drive and I just need aaa and bbb.
FYI: I need the script so that I can use it to monitor zabbix agents and see if the specific software is on the computer so I can run this script on the Zabbix Server.
You are looking for the -include parameter:
Get-ChildItem C:\ -recurse -include "aaa.exe", "bbb.exe"
#Martin Brandl has already answered your first question. For the second part of your question, you can do something like this -
$AllDrives = Get-PSDrive -PSProvider 'FileSystem'
foreach ($drive in $AllDrives)
{
Get-ChildItem -path $drive.Root -recurse -include "aaa.exe", "bbb.exe"
}
Or you can do it in one line -
Get-PSDrive -PSProvider 'FileSystem' | % {Get-ChildItem -path $_.Root -recurse -include "aaa.exe", "bbb.exe"}
You can change the -PSProvider parameter if your executables are on other drives other than FileSystem drives.

How to check if a directory contains another directory in Powershell?

I am looking for a simple (if possible) way in powershell to check if a directory contains at least 1 other directory.
$dir = '.' # specify the directory to investigate
$hasAnySubdir = (Get-ChildItem -Force -Directory $dir).Count -gt 0
-Directory (PSv3+) ensures that Get-ChildItem only enumerates sub-directories, and -Force ensures that even hidden subdirectories are included.

New-Item recursive registry keys

With PowerShell you can run a command like this
ni c:/foo/bar -type directory
and it will create foo and bar as necessary. However if you run
ni hklm:software/classes/firefoxhtml/shell/edit/command -type directory
all keys but the last must exist or an error will be generated. Can PowerShell generate the parent keys as needed?
I was just missing the -force parameter
New-Item hklm:software/classes/firefoxhtml/shell/edit/command -Force
Using -Force will also remove everything under the key if it already exists so a better option would be
if(!(Test-Path $path)){
New-Item $path -Force;
}

Recursive listing of a particular file type in Windows PowerShell

I was trying to recursively list using ls all the *.py files in a directory in Windows PowerShell. It didn't work. I checked up on the man page and then online page that -Recurse flag doesn't accept wildcards so *.py wouldn't work.
I want to know whether there is a simple in-built way to recursively list the files of a particular file extension in a directory using Windows PowerShell 2.0?
I am a newbie in PowerShell and don't want to indulge in Shell scripting at this stage. So please recommend in-built commands, flags, etc. only, if any.
By commands I means the in-built keywords of the PowerShell. I am not sure if that is proper vocabulary for PowerShell.
Use the Filter parameter:
ls $path -filter *.py -recurse
This will do the trick for you.
gci -Recurse | ? {$_.name -match ".py"}
or
gci -Recurse -Include *.py
ls is an alias for Get-ChildItem. (So is dir and gci). A simple Google will give you all kinds of examples but the thing to know is -include (or -exclude) are built in parameters that will show (or not show) file types you are looking for. An additional parameter, -filter, can also be used for partial file types. The switch parameter -recurse checks the contents of subfolders in the directory as well. Example01:
gci -path \$computername\$directory -Include *.py -recurse
Example02:
gci -path \$computername\$directory -Filter *.py -recurse
I like to suppress errors so a full example would look like this:
gci -path \Svr01\C$ -Include *.py -recurse -erroraction SilentlyContinue
I don't see how anybody would have got this working at any time.
gci -recurse works fine and lists all
gci -filter *.txt works fine, lists all .txt files but does not recurse
gci -filter *.txt -recurse returns either only .txt files from root or nothing.
It seems to apply the *.txt filter to the directory names therefor seeing no directories and thus does not recurse at all.

Resources