Book Image

SQL Server 2014 with PowerShell v5 Cookbook

By : Donabel Santos
Book Image

SQL Server 2014 with PowerShell v5 Cookbook

By: Donabel Santos

Overview of this book

Table of Contents (21 chapters)
SQL Server 2014 with PowerShell v5 Cookbook
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

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 contents of file
    notepad $fullPath
    
    #merge file contents
    $newContent = Get-Content "C:\Temp\processes.txt"
    Add-Content $fullPath $newContent
    #show contents of file
    notepad $fullPath
    
    #delete file
    Remove-Item $fullPath

How it works...

Here...