Handling File Systems with PowerShell

Shammi Kolonne
6 min readDec 12, 2021

--

Working with file systems and manipulating files are activities we engage in on a regular basis. It is also possible to get much of these tasks done easily through PowerShell cmdlets.

This article will be discussing about various cmdlets you can use when working with folders and different file types. First, I will discuss about file path validation and will move on to displaying contents within directories. Next, I will look into copying files and directories between different locations and then into creating new files and directories. Finally, deleting files and folders will be discussed. Most commands that I have explained in this article are not meant to only handle files and directories but other items like registries as well. But since this article focuses only on files and directories, I will only look into that aspect.

File Path Validation

It goes without saying that, to navigate through folders or to access files, a correct path to that specific folder/file should be provided. Be it the absolute path or the relative path, it is always better to start off by making sure the path is valid.

Test-Path –Path E:\Files

This command checks if all the elements of a given path, in this case the E: drive and the ‘Files’ folder, exists and returns true if they do. If even one of them is missing, the command returns false.

The -PathType parameter can be used together with this command to determine the type of the last element of a path.

Test-Path -Path E:\Files\test.txt -PathType Leaf

The above will return true since the path E:\Files\test.txt leads to a file. If the last element was a folder and not a file, this would have returned false. Another parameter is Container that can be used instead of Leaf, which would return true if the path leads to a folder and false otherwise.

Using the -Include parameter can help you find if a folder contains files of a specific type as shown below.

Test-Path " E:\Files\*" -Include *.txt

The above will return true if the folder ‘Files’ contains .txt files inside it. If there were no .txt files within the folder, it would have returned false. One important thing to note is that the path should have a \* at the end of it for all items inside the folder to be considered.

Likewise, -Exclude parameter can be used to find if there are files other than the type being specified.

Split-Path “E:\Files\test.txt” –Leaf 

The -Leaf parameter returns the file or folder name at the end of the given path. There are several other parameters you can use to get certain parts of a path.

  • -Qualifier — Returns only the drive of the given path. The path E:\Files\test.txt would return E: .
  • -NoQualifier — Returns the path without the drive. The path E:\Files\test.txt would return \Files\test.txt .
  • -Parent — Returns only the parent folders of the path. The path E:\Files\test.txtwould return E:\Files. But as -Parent is the default of the command Split-Path, running the command without giving a flag would also return the parent folder.
  • -LeafBase — Returns only the name of the last file in the path leaving out the extension of the file. The path E:\Files\test.txt would only return test.
  • -Extension — Returns only the extension of the last file in the path. The path E:\Files\test.txt would only return .txt.

The last two parameters, -LeafBase and -Extension can be used only with PowerShell versions 6.0 and above.

Another parameter is -IsAbsolute, which would return true if the path is an absolute path and false if it is relative.

Displaying Content within Directories

The Get-childItem cmdlet can be used to display files and directories in a specified path in the console as shown in below example.

Get-ChildItem

This cmdlet displays details like Mode, last write time, length and name of each file and folder. Mode refers to attributes of the file/folder and can be one of the following.

  • l (link)
  • d (directory)
  • a (archive)
  • r (read-only)
  • h (hidden)
  • s (system)

If only the names need to be listed, the cmdlet can be altered with a -Name parameter as follows.

get-childItem E:\testFolder -Name

It is also possible to get contents in the sub-directories as well.

Accessing sub-directories

The -Exclude and -Include parameters can be used with this cmdlet in a similar manner as was described with Test-Path cmdlet.

Copying Files and Folders

The Copy-Item cmdlet can be used to copy files from one location to another.

copy-item E:\testFolder\test1\subTest.txt -Destination E:\testFolder\destTest

This command will make a copy of the file ‘subTest.txt’ inside the destination folder, ‘destTest’. It is also possible to copy all contents of a specified directory as shown below.

copy-item E:\testFolder\test1\* -Destination E:\testFolder\destTest

Contents of folder ‘test1’ will be copied to folder ‘destTest’.

If the given destination folder doesn’t exist, a new folder of the given name will be created before the contents are copied to it.

We can use the -Recurse parameter to copy files and folders within sub-directories as well.

Creating New Files and Folders

Creating new files and folders is also possible with PowerShell. The New-Item cmdlet is used for this.

New-Item -Path . -Name “dummy1.txt” -ItemType “file” -Value “dummy file.”

Above command will create a new text file named ‘dummy1’ with the text “dummy file” as its content. Since the path is given as ., the file will be created in the current location. Some other path can also be specified with the -Path parameter.

The same command can be used to create directories by giving a directory name and specifying the item type as “directory”.

New-Item -Path “c:\” -Name “dummyDir” -ItemType “directory”

Another way to create directories is shown below.

New-Item -ItemType “directory” -Path “E:\test\dummyDir”

A new folder with the name “dummyDir” will be created in E:\test.

Deleting Files and Folders

Finally, let’s discuss how deleting files can be done with PowerShell. This is possible using the Remove-Item cmdlet.

remove-item E:\testFolder\testword1.docx

Above command will remove the “testword1” word document in the E:\testFolder location.

Let’s look at a few examples.

First command will remove all items inside the “testFolder”. The second will remove all file types inside the “test1” folder. Directories won’t be removed due to the . in the middle of *.*. The third will only remove all the text files inside “testFolder”.

The -Recurse parameter together with the combination of a pipeline can be used to recursively delete sub-directories inside a directory.

Get-ChildItem * -Recurse | Remove-Item

Conclusion

We can make use of PowerShell cmdlets to handle file and folder operations easily. This article has discussed about file path validation, displaying directory contents, copying, creating and deleting files. Since this article only discusses the most basic operations, readers are encouraged to refer the PowerShell documentation to learn more.

References

--

--

Shammi Kolonne
Shammi Kolonne

Written by Shammi Kolonne

Software Engineer @ WSO2 | Graduand from CSE, University of Moratuwa

No responses yet