Basics with Windows Power Shell by Prometheus MMS - HTML preview

PLEASE NOTE: This is an HTML preview only and some elements such as links or page numbers may be incorrect.
Download the book in PDF, ePub, Kindle for a complete version.

2. FILES AND FOLDERS

The following articles provide a brief introduction to carrying out file system management tasks by using Windows PowerShell; these tasks include such things as creating, renaming, copying, and deleting files and folders. As is so often the case with Windows PowerShell, the code snippets found in these articles can either be included within a Windows PowerShell script or typed directly into the Windows PowerShell console.

img6.png Copy Files or Folders

img6.png Create a New File or Folder

img6.png Delete a File or Folder (Or Other Objects)

img6.png Move a File or Folder

img6.png Rename a File or Folder

img6.png Replicate (and Improve Upon) the DIR Command

img6.png Retrieve a Specific Item

img6.png Verify the Existence of a File or Folder

img6.png Copying Files or Folders

Want to a copy a file or folder to a new location? Then you want the Copy-Item cmdlet. For example, here’s a command that copies the file Test.txt from the C:\Scripts folder to the C:\Test folder:

Copy-Item c:\scripts\test.txt c:\test

Want to copy all the items in C:\Scripts (including subfolders) to C:\Test? Then simply use a wildcard character, like so:

Copy-Item c:\scripts\* c:\test

You’re way ahead of us: yes, this next command copies only the .txt files in C:\Scripts to C:\Test:

Copy-Item c:\scripts\*.txt c:\test

Finally, this command puts a copy of the folder C:\Scripts inside the folder C:\Test; in other words, the copied information will be copied to a folder named C:\Test\Scripts. Here’s the command:

Copy-Item c:\scripts c:\test - recurse

Incidentally, the -recurse parameter is absolutely crucial here; leave it out and a Scripts folder will be created in C:\Test, but none of the files and folders in C:\Scripts will be copied to the new location; you’ll create a C:\Test\Scripts folder, but there won’t be anything in it. Copy-Item Aliases

cpi

cp

copy