Managing files and folders in PowerShell

Hello Everyone,

This blog is in continuation of “What is PowerShell?" and “Intro to tools, commands and modules in PowerShell”. We prefer you should complete reading those blogs first, then continue here as all blogs are related.

In this will explore about Managing files and folders in PowerShell.

Regardless of your niche in the IT industry, chances are some part of your daily grind involves managing files and folders in some way. Whether it’s moving folders to another location on a server, archiving log files, or looking for large files; almost every system administrator spends part of their day managing files and folders. In cases where repetitive tasks are being repeated on multiple files, or the same set of tasks are run repeatedly, automation through PowerShell can be a real time saver.

Finding files and folders One of the first command line tools administrators would learn in the olden days of computers was the dir command. For those new to the game, dir would list the files and folders contained within the specified directory. PowerShell features a similar command in the form of the Get-ChildItem cmdlet. Get-ChildItem allows you to quickly build a listing of the files in a directory in a way that you can then act on these files either through a piped command or by assigning the output to a variable.

At its most basic, Get-ChildItem can be used simply by providing a path, either through the pipeline, using the -Path parameter, or immediately following the cmdlet name. To tune the response returned by Get-ChildItem, it is key to look at some of the parameters made available by the cmdlet.

The -Filter parameter is one way you can search for files. By default, the Get-ChildItem cmdlet only returns direct children of the target directory. This functionality can be expanded using the -Recurse switch, which recursively searches directories contained within the current folder.

In PowerShell 4.0 Get-ChildItem adds the ability to limit the result to either a file or folder by using the –File or –Directory switches. Prior versions of PowerShell had to pipe the result to Where-Object , filtering on the PSIsContainer property to make this determination. An example of both techniques being used to return the folders contained in C:Users is shown here:

Get-ChildItem C:Users -Directory Get-ChildItem C:Users | Where-Object {$_.PSIsContainer –eq $true}

In order to discover hidden or system files the -Force switch must be used. Get-ChildItem in PowerShell 4.0 and above can also be used to return only those files which are hidden, read only, or system files using the -Hidden , -ReadOnly , and –System switches respectively. The same functionality can be achieved in prior versions by filtering on the Mode property using Where-Object :

Get-ChildItem C:Users | Where-Object {$_.Mode -like ‘R’}

Checking if a file exists

Often when working with files, all we need to know is whether a file exists or a folder path is valid. PowerShell offers a cmdlet to perform this validation in the form of Test-Path , which returns either a true or false value.

Test-Path is often useful as a precautionary step prior to attempting to copy or delete a particular file.

Copying, moving, and deleting files

As you would expect, PowerShell is fully capable of performing standard file operations on numerous objects in a single pass. The Copy-Item cmdlet can be used to copy one or more files or folders from one location, identified by the -Path parameter, to the location specified by the -Destination option.

Likewise, the Move-Item cmdlet is used when relocating a file or folder. When a folder structure is being copied or moved, the -Recurse switch should be used in order to have the cmdlet perform the action on the folder and its contents. In some cases, the -Force switch is needed, such as when a read-only file is being overwritten by the copy operation.

Files and folders can be deleted using the Remove-Item cmdlet. As is the theme with many of the cmdlets discussed here, the -Force switch must be used when hidden or read-only files are encountered, and -Recurse should be used when deleting a folder and its contents.

Using PowerShell -WhatIf and -Confirm

It’s been said of scripts that they enable people to do stupid things extremely quickly. Before you pull the trigger on a big delete operation, make use of the -WhatIf switch offered for the majority of the cmdlets discussed here.

The -WhatIf switch allows you to see what will happen if you actually run the script or command, without the potential negative effects of deleting critical business data. It’s also worth noting that -WhatIf isn’t limited to file operations, it’s widely used throughout PowerShell.

For scripts that you intend to manually run or worse, have a subordinate manually run, consider using -Confirm . This allows you to require user interaction prior to the operation actually taking place. Often this is preferable to simply assuming everything is ready to go (file backups complete, replication disabled, etc.) prior to large file operations being initiated.

Thankyou.