How to create, delete or list the files from Windows Powershell

Table of Contents

Introduction

In this article we will cover the steps to use to create or delete a file 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 file in windows powershell 

In this section we will cover the creation of  new files 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 

PS C:\Users\Public\Discovering-systems> new-item test1.txt

    Directory: C:\Users\Public\Discovering-systems

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        6/27/2023   7:03 AM              0 test1.txt

ni command 

Similarly we can create a file using the ni command as it is the alias for the new-item command 

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 test2.txt

    Directory: C:\Users\Public\Discovering-systems

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        6/27/2023   7:07 AM              0 test2.txt

How to delete the files from the Windows powershell 

In this section we will cover how to delete the created files 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 

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 files created in the earlier section using the del and remove-item commands

PS C:\Users\Public\Discovering-systems> del .\test1.txt
PS C:\Users\Public\Discovering-systems> remove-item .\test2.txt

How to check the files or directories in the current working directory  

We can use the get-childitem command or its alias ls command to check for the files and folders inside the present working directory 

Get-alias command helps us with identifying the alias associated with the various powershell commands 

PS C:\Users\Public\Discovering-systems> get-alias -name ls

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           ls -> Get-ChildItem

In this case,  we already deleted the two files, so nothing returned on the screen. If there were files or folders in the present in the current working directory then we should see some listed  

PS C:\Users\Public\Discovering-systems> Get-ChildItem
PS C:\Users\Public\Discovering-systems> ls
PS C:\Users\Public\Discovering-systems>

Example when there were files present 

PS C:\Users\Public\Discovering-systems> get-childitem

    Directory: C:\Users\Public\Discovering-systems

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        6/27/2023   7:52 AM             36 test1.txt

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

Conclusion

In this article we were able to explore the ways in which we can use the powershell to create , delete or list the files. 

If you are interested in more windows powershell related articles from us , please go through the following articles  

Leave a Comment

Your email address will not be published. Required fields are marked *