Files and Folder Operations

Powershell For FileSystem For administrators it is important know commands related to file system. Once administrator know about his day to day he need to work on it. Most of us have started their career with playing around file system. Even if we’re working with server applications such as SQL or Exchange Server, as everything thing is built on files, we still need to interact with the file system. We will cover here most of the common administrative tasks regarding the file system.

Let’s see lists the cmdlets that can be used to work with file system also we will see the example. Although names are self explanatory lets see description about it.

Powershell Create File

# Using PowerShell commnads to create a new file PS>$Location = "C:\test" PS>New-Item -Path $Location -Name "FirstCreateFile.txt" -ItemType File -Force

Copy File

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.

# Here I define the folders and the extensions to look at. $folder1 = ls "C:\powershell-scripts\from\" -recurse -include "*.pdf" $destinationfolder = "C:\powershell-scripts\to\" Copy-Item -Path $folder1 -Destination $destinationfolder

Read and Rename File

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. In order to tune the response returned by Get-ChildItem it is key to take a look at some of the parameters made available by the cmdlet.

PS> Get-Content c:\powershell-scripts\test.txt PS> Rename-Item c:\scripts\test.txt new_name.txt

List the file files

# PowerShell script to list the DLL files under the system32 folder $Dir = get-childitem C:\windows\system32 $List = $Dir | where {$_.extension -eq ".dll"} $List