
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check If the File is Empty Using 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.
For example, We have a test2.txt text file which has whitespaces.
Example
[String]::IsNullOrWhiteSpace((Get-content C:\Test2.txt))
Output
True
But if you have a file like CSV which contains few headers but the data is empty, in that case, Get-Content will show the wrong output because it will consider headers. For example,
Example
[String]::IsNullOrWhiteSpace((Get-content C:\Temp\NewUsers.csv))
Output
False
Because the file has headers.
PS C:\> Get-Content C:\Temp\NewUsers.csv Name,FirstName,Surname,EMPNumber,Country
In that case, we can use the Import-CSV command inside the brackets.
Example
[String]::IsNullOrWhiteSpace((Import-Csv C:\Temp\NewUsers.csv))
Output
True
Advertisements