CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

PowerShell – Managing Files and Folders

PowerShell – Managing Files and Folders, can be done through PowerShell commands or scripts; with ease. In my previous article, “PowerShell : Display files and folders list“, I have explained how to display files and folders list. In this article, I am going to explain; few more PowerShell commands; which are useful to manage the files and folders.

Below is the list of commands which are helpful to manage the files and folders:

  • New-Item
  • Copy-Item
  • Move-Item
  • Remove-Item
  • Rename-Item and
  • Invoke-Item

New-Item cmdlet

New-Item cmdlet is the command used to create a new file or a directory. We need to specify while creating an item; whether it is a “file” or a “directory” through its parameter, “-ItemType“.

Here is the example to create a new file “C:\New-File.txt“:

PS C:\> New-Item "C:\New-File.txt" -ItemType "file"

    Directory: C:\

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         8/25/2018   8:59 PM          0 New-File.txt

It’s’ so simple right.? Yes. But what I will try here is; will use these commands with Parameters for more readability. Most of us, already know; some of the common (non PowerShell) commands; to manage the files & folders; but mostly, we do not know how to use these PowerShell commands to do the same kind of job.

Here is an example to create a new directory “C:\New_Dir“:

Observe the Parameters, -Path, -Name & -ItemType. In PowerShell parameters are specified prefixing hyphen (“-“) symbol; and the parameter values will be passed, after the parameters.

PS C:\> New-Item -Path . -Name "New_Dir" -ItemType "directory"

    Directory: C:\

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----         8/25/2018   8:57 PM            New_Dir

If already an item exists, and attempt to create the same item: For example, if you attempt to create a folder again “C:\New_Dir”, it will show below Error message:

PS C:\> New-Item -Path . -Name "New_Dir" -ItemType "directory"
New-Item : An item with the specified name C:\New_Dir already exists.
At line:1 char:1
+ New-Item -Path . -Name "New_Dir" -ItemType "directory"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceExists: (C:\New_Dir:String) [New-Item], IOException
    + FullyQualifiedErrorId : DirectoryExist,Microsoft.PowerShell.Commands.NewItemCommand

Copy-Item cmdlet

Copy-Item cmdlet is useful to copy the files or folders from one location to another location:

PS C:\> Copy-Item -Path "C:\New-File.txt" -Destination C:\Another-Location\

Note that, if it is a folder copy; all files within the folder will be moved to a new location.

We can use wildcards (“*”, “?”),  to copy multiple items at a time. Below command, copies all “.txt” files that are in the current directory; to the specified destination:

PS C:\> Copy-Item -Path "*.txt" -Destination C:\Another-Location\

Move-Item cmdlet

Move-Item cmdlet is useful to move the file(s) or folder(s) from one location to another location. Note that, all files within the folder will be moved to the new location:

PS C:\> Move-Item -Path C:\New_Dir -Destination C:\Another_Location\New_Dir

To move a file, from one location to another location:

PS C:\> Move-Item -Path C:\New-File.txt -Destination C:\Another_Location\New-File.txt

If the new location doesn’t exist; it shows below Error.

PS C:\> Move-Item -Path C:\New-File.txt -Destination C:\Different_Location\New-File.txt
Move-Item : Could not find a part of the path.
At line:1 char:1
+ Move-Item -Path C:\New-File.txt -Destination C:\Different_Location\New-File.txt
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (C:\New-File.txt:FileInfo) [Move-Item], DirectoryNotFoundException
    + FullyQualifiedErrorId : MoveFileInfoItemIOError,Microsoft.PowerShell.Commands.MoveItemCommand

Move-Item will move the item(s) to the new location and deletes the item(s) from its’ old location.

We can also use wildcards (“*”, “?”), to move multiple items (s) or folder(s) at a time to the specified destination.

Remove-Item cmdlet

Remove-Item cmdlet, is another command which is used to delete the file(s) or folder(s).

Here is the command to delete the file:

PS C:\> Remove-Item -Path C:\New-File.txt

To delete a folder, if it contains, files; PwerShell prompts to delete all the file(s) or folder(s) within it.

PS C:\> Remove-Item -Path C:\Another_Location

Confirm
The item at C:\Another_Location 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"): Y

We can use, wildcards (“*”, “?”) to delete multiple file(s) or folder(s) at a time.

Rename-Item cmdlet

Rename-Item cmdlet, is useful to rename the file or folder.

PS C:\> Rename-Item -Path "C:\New-File.txt" -NewName "C:\New-Renamed-File.txt"

Renaming can be done, by using Move-Item cmdlet also.

Invoke-Item cmdlet

Invoke-Item cmdlet, is useful to invoke a default action on the given item. For example, to open a text file:

PS C:\> Invoke-Item .\New-File.txt

These are cmdlets, we use in PowerShell to manage the file(s) or folder(s).

We will discuss more PowerShell commands, in my upcoming Articles.

Please don’t forget to give the feedback through the below Comments.

(Raju)

PowerShell – Managing Files and Folders

Leave a Reply

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

Scroll to top