Table of contents
- Introduction
- How to create a new directory in windows powershell
- How to delete the directories from the Windows powershell
- Conclusion
Introduction
In this article we will cover the steps to use to create or delete a directory directly from the windows powershell. We will also see the alias associated with those commands so that we can use it for doing the same action
How to create a new directory in windows powershell
In this section we will cover the creation of new directories from powershell using the new-item command and its alias ni command
new-item command
Using the new-item command we can create both the files and the directories , By default without any options it creates a file. If used with the option “-itemtype directory” it creates the directory
PS C:\Users\Public\Discovering-systems> new-item -itemtype directory test-directory
Directory: C:\Users\Public\Discovering-systems
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 7/6/2023 6:55 AM test-directory
ni command
Similarly we can create a directory using the ni command as it is the alias for the new-item command. Note that we need to use the same option “ -itemtype directory “ to create the directories as without any options it creates the files
Get-alias command helps us with identifying the alias associated with the various powershell commands
PS C:\Users\Public\Discovering-systems> get-alias -name ni
CommandType Name Version Source
----------- ---- ------- ------
Alias ni -> New-Item
PS C:\Users\Public\Discovering-systems> ni -itemtype directory test-directory-2
Directory: C:\Users\Public\Discovering-systems
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 7/6/2023 6:57 AM test-directory-2
How to delete the directories from the Windows powershell
In this section we will cover how to delete the directories from the powershell using the remove-item command and its alias del command
Get-alias command helps us with identifying the alias associated with the various powershell commands. We can see that the 3 aliases like rm , rmdir , del are mapped to the same command remove-item . we can use any of them to do the same action
PS C:\Users\Public\Discovering-systems> Get-Alias -name rm
CommandType Name Version Source
----------- ---- ------- ------
Alias rm -> Remove-Item
PS C:\Users\Public\Discovering-systems> Get-Alias -name rmdir
CommandType Name Version Source
----------- ---- ------- ------
Alias rmdir -> Remove-Item
PS C:\Users\Public\Discovering-systems> Get-Alias -name del
CommandType Name Version Source
----------- ---- ------- ------
Alias del -> Remove-Item
In the following example , we have deleted both the directories created in the earlier section using the del and remove-item commands . Please note that it got deleted without any errors as they were empty directories
PS C:\Users\Public\Discovering-systems> del .\test-directory\
PS C:\Users\Public\Discovering-systems> remove-item .\test-directory-2\
PS C:\Users\Public\Discovering-systems> ls
Directory: C:\Users\Public\Discovering-systems
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 6/27/2023 7:52 AM 36 test1.txt
Let’s say we have some files under those directories , then we will see a prompt asking to confirm whether we want to remove all the files under that recursively , then we can select option yes by typing “y” and it should delete them all
PS C:\Users\Public\Discovering-systems> remove-item .\test-directory-2\
Confirm
The item at C:\Users\Public\Discovering-systems\test-directory-2\ has children and the Recurse parameter was not
specified. If you continue, all children will be removed with the item. Are you sure you want to continue?
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):
If you would like to delete the directories and the files recursively without seeing the prompt , we can use the recurse option along with the remove-item command
PS C:\Users\Public\Discovering-systems> rm .\test-directory-2\ -recurse
Conclusion
In this article we were able to explore the ways in which we can use the powershell to create , or delete directories
If you are interested in more windows powershell related articles from us , please go through the following articles