Powershell

How to Quietly Remove a Directory With Content in PowerShell

As we know, PowerShell is a Windows administrator tool used to perform administrative tasks with full authority. More specifically, it can be utilized to delete folders and subfolders quietly. When we use the term “quietly remove a directory”, it means deleting a directory without any prompt and not moving it to the Recycle bin. For this purpose, PowerShell uses different cmdlets and parameters to remove a directory.

This blog will overview the procedures to remove the directory quietly.

How to Quietly Remove a Directory with the Content in PowerShell?

These are the methods that can be approached to remove a directory quietly.

Method 1: Quietly Removing a Directory With the Content in PowerShell Using “Remove-Item” Command

First method to remove a directory in PowerShell quietly is the usage of the “Remove-Item” cmdlet. This cmdlet is used to remove/delete a specified item/folder by the user.

Syntax

Here is the basic syntax for utilizing the Remove-Item command:

> Remove-Item -LiteralPath "FolderPathHere" -Force -Recurse

Here:

  • -LiteralPath” parameter is used to specify the path of the directory.
  • -Force” parameter is used to remove the read-only files.
  • -Recurse” parameter is used to force the delete process to also delete the sub-folders.

Example

In this example, we will remove the “C:\Doc” directory quietly using the Remove-Item command as follows:

> Remove-Item -LiteralPath "C:\Doc" -Force -Recurse

Method 2: Quietly Removing a Directory With the Content in PowerShell Using “rm” Command

Another method to remove the directory with content quietly is utilizing the “rm” cmdlet. This cmdlet is a cmdlet on Unix-like operating systems to delete files and folders. Moreover, it is also utilized in Windows PowerShell for the same purpose of deleting files.

Syntax

This is the syntax of the “rm” cmdlet:

> rm /path -r -force

In the given syntax, “-r” is an alias to the “-Recurse” parameter. Combining it with the “-force” parameter will delete all of the files recursively.

Example

Now, utilizing the stated syntax, we will remove the content with the “C:\New” directory quietly:

> rm C:\New -r -force

That was all about removing files and folders without prompting the user.

Conclusion

The directory with the content in PowerShell can be removed quietly by using the “Remove-Item” and “rm” cmdlets. Remove-Item cmdlet uses the directory path and then uses the “-Force” and “-Recurse” parameters. While “rm” is the alias of the “Remove-Item” cmdlet, it uses all the alias parameters, such as “-r” and “-f”. This blog has presented a detailed guide to quietly removing a directory in PowerShell.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.