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

Managing folders


In this recipe, we will explore different cmdlets that support folder management.

How to do it...

Let's take a look at different cmdlets that can be used for folders:

  1. Open PowerShell ISE as an administrator.

  2. Add the following script and run it:

    #list folders ordered by name descending
    $path = "C:\Temp" 
    
    #get directories only
    Get-Childitem $path | 
    Where-Object PSIsContainer
    
    #create folder
    $newFolder = "C:\Temp\NewFolder"
    New-Item -Path $newFolder -ItemType Directory -Force
    
    #check if folder exists 
    Test-Path $newFolder
    
    #copy folder
    $anotherFolder = "C:\Temp\NewFolder2"
    Copy-Item $newFolder $anotherFolder -Force
    
    #move folder
    Move-Item $anotherFolder $newFolder
    
    #delete folder
    Remove-Item $newFolder -Force -Recurse

How it works...

Here are some cmdlets that support folder manipulation:

Cmdlet

Description

Get-ChildItem

This lists all directories in a path:

#get directories only Get-Childitem $path | Where PSIsContainer

Test-Path

This checks whether a folder exists:

Test...