Manipulating files

In this recipe, we will take a look at different cmdlets that help you manipulate files.

How to do it...

Let's explore different ways to manage files:

  1. Open PowerShell ISE as an administrator.
  2. Add the following script and run it:
    #create file $timestamp = Get-Date -format "yyyy-MMM-dd-hhmmtt" $path = "C:\Temp\" $fileName = "$timestamp.txt" $fullPath = Join-Path $path $fileName New-Item -Path $path -Name $fileName -ItemType "File" #check if file exists Test-Path $fullPath #copy file $path = "C:\Temp\" $newFileName = $timestamp + "_2.txt" $fullPath2 = Join-Path $path $newFileName Copy-Item $fullPath $fullPath2 #move file $newFolder = "C:\Data" Move-Item $fullPath2 $newFolder #append to file Add-Content $fullPath "Additional Item" #show ...

Get SQL Server 2014 with PowerShell v5 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.