Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell test if folder empty

Tags:

powershell

In Powershell, how do I test if a directory is empty?

like image 888
Colonel Panic Avatar asked May 11 '12 11:05

Colonel Panic


People also ask

How do I check if a file is empty in PowerShell?

To check if the file is empty using PowerShell, we can use the string method called IsNullorWhiteSpace(). This method provides result true if the file is empty or only contains the white spaces otherwise false.

How do you check if a directory is empty in Windows?

To check whether a directory is empty or not os. listdir() method is used. os. listdir() method of os module is used to get the list of all the files and directories in the specified directory.

How do you check if a file exists in a folder using PowerShell?

To use PowerShell to check if a file exists, you use the Test-Path Cmdlet. However, you can either enter the file path directly to the Test-Path command or save the file in a variable. Then use the variable in Test-Path.

How do I delete a folder in PowerShell?

Use the Delete() Method Every object in PowerShell has a Delete() method and you can use it to remove that object. To delete files and folders, use the Get-ChildItem command and use the Delete() method on the output.


2 Answers

If you are not interested in hidden or system files you can also use Test-Path

To see if it exists a file in directory .\temp you can use :

Test-Path -Path .\temp\* 

or shortly :

Test-Path .\temp\* 
like image 65
JPBlanc Avatar answered Sep 21 '22 15:09

JPBlanc


Try this...

$directoryInfo = Get-ChildItem C:\temp | Measure-Object $directoryInfo.count #Returns the count of all of the objects in the directory 

If $directoryInfo.count -eq 0, then your directory is empty.

like image 32
Boeckm Avatar answered Sep 21 '22 15:09

Boeckm