Copy-Item: The Copy-Item cmdlet copies an item from one location to another in a namespace. Copy-Item does not delete the items being copied. When the cmdlet is used with filesystems, it can be used to copy files and directories, and when used with Windows Registry, it can be used to copy registry keys and entries.
The following command will copy the file cmdout.log.txt to the C:\Presentation directory. The command does not delete the original file.
The following command copies the entire content of the Log directory into the Backup directory. If the source directory contains files in subdirectories, those subdirectories will be copied with their file trees intact. The Container parameter is set to true by default. This preserves the directory structure.
The default alias for the Copy-Item cmdlet is copy; you can use the cmdlet alias as follows:
Get-Item: The Get-Item cmdlet gets the item at the specified location. It does not get the contents of the item at the location unless you use a wildcard character (*) to request all the contents of the item. In the filesystem, the Get-Item cmdlet gets files and folders:
This command gets the current directory. The dot character (.) represents the item at the current location (not its contents):
The following command gets the current directory of C:\:
The object that is retrieved represents only the directory, not its contents:
The following command gets the items in C:\:
The wildcard character (*) represents all the items in the container, not just the container. You should see the following screenshot now:
The following command gets the LastAccessTime property of the C:\Windows directory. LastAccessTime, as shown in the following command, is just one property of the file system object:
You should now see the following screenshot:
The following command gets items in the Windows directory with names that include a dot (.), but do not begin with w*. This command works only when the path includes a wildcard character (*) to specify the contents of the item:
You should now get the following screenshot:
The default alias for the Get-Item cmdlet is gi; you can use the cmdlet alias as follows:
Get-ItemProperty: The Get-ItemProperty cmdlet gets the properties of the specified items. For example, you can use Get-ItemProperty to get the value of the LastAccessTime property of a file or folder object. You can also use Get-ItemProperty to view registry entries and their values:
The previous command gets information about the C:\Windows directory:
The following command gets the properties of the C:\Windows\bfsvc.exe file:
The result is piped to the Format-List cmdlet to display the output as a list:
The default alias for the Get-Item cmdlet is gi; you can use the cmdlet alias as follows:
Invoke-Item: The Invoke-Item cmdlet performs the default action on the specified item. For example, it runs an executable file or opens a document file in the application associated with the document file type. The default action depends on the type of item and is determined by the Windows PowerShell provider that provides access to the data.
The following command opens the file alias counter.csv in Microsoft Excel:
In this case, opening in Excel is the default action for .csv files. You should now see the following screenshot:
The following command opens all of the Microsoft Office Excel spreadsheets in the C:\Documents and Settings\sample\My Documents folder:
Each spreadsheet is opened in a new instance of Excel. In this case, opening in Excel is the default action for the .xls files. You should now see the following screenshot:
The default alias for the Invoke-Item cmdlet is ii; you can use the cmdlet alias as follows:
Move-Item: The Move-Item cmdlet moves an item, including its properties, contents, and child items, from one location to another. The locations must be supported by the same provider. For example, it can move a file or subdirectory from one directory to another, or move a registry subkey from one key to another. When you move an item, it is added to the new location and deleted from its original location.
The previous command moves the Test.txt file from the C:\ drive to the E:\Temp directory and renames it from test.txt to tst.txt, as shown in the following screenshot:
The following command moves all of the text files (*.txt) in the current directory (represented by a dot (.)) to the C:\Logs directory:
You should now see the following screenshot:
The following command moves all of the text files from the current directory and all subdirectories, recursively, to the C:\TextFiles directory:
You should now see the following screenshot:
The default alias for the Move-Item cmdlet is move, mi, or mv; you can use the cmdlet alias as follows:
New-Item: The New-Item cmdlet creates a new item and sets its value. The types of items that can be created depend upon the location of the item. For example, in the filesystem, New-Item is used to create files and folders. In the registry, New-Item creates registry keys and entries.
The previous command creates a text file named testfile1.txt in the c:\vinith directory. The dot (.) in the value of the Path parameter indicates the current directory. The quoted text that follows the Value parameter is added to the file as content:
The following command creates a directory named Logfiles in the C:\ directory:
The ItemType parameter specifies that the new item is a directory, not a file or some other filesystem object, as shown in the following screenshot:
The following command uses the New-Item cmdlet to create files in two different directories:
Because the Path parameter takes multiple strings, you can use it to create multiple items. You can now see the following screenshot:
The default alias for the New-Item cmdlet is ni. You can use the cmdlet alias as follows:
Remove-Item: The Remove-Item cmdlet deletes one or more items. Because it is supported by many providers, it can delete many different types of items, including files, directories, registry keys, variables, aliases, and functions. In filesystem drives, the Remove-Item cmdlet deletes files and folders.
This command deletes all the files with names that include a dot (.) from the C:\Test directory. Because the command specifies a dot, the command does not delete directories or files with no file name extension. You should now see the following screenshot:
The following command deletes a file that is both hidden and read-only:
It uses the Path parameter to specify the file. It uses the Force parameter to give permission to delete it. Without Force, you cannot delete read-only or hidden files.
The default alias for the Remove-Item cmdlet is del, erase, rd, ri, rm, and rmdir. You can use the cmdlet alias as follows:
Get-Acl: The Get-Acl cmdlet gets objects that represent the security descriptor of a file or resource. The security descriptor contains the access control lists (ACLs) of the resource in an array. The ACL specifies the permissions that users and user groups have to access the resource.
The following command gets the security descriptor of the C:\Windows directory:
You should now see the following screenshot:
Set-Acl: The Set-Acl cmdlet changes the security descriptor of a specified item, such as a file or a registry key, to match the values in a security descriptor that you supply. To use Set-Acl, use the Path parameter to identify the item whose security descriptor you want to change. Then, use the AclObject parameters to supply a security descriptor that has the values you want to apply.
The following commands copy the values from the security descriptor of the 123.csv file to the security descriptor of the 123.html file:
When the execution of the commands is complete, the security descriptors of the 123.csv and 123.html files are identical. The following screenshot shows the usage of the commands:
The following command is almost the same as the command in the previous example, except that it uses a pipeline operator to send the security descriptor from a Get-Acl command to a Set-Acl command:
The following commands apply the security descriptors in the 123.csv file to all text files in the C:\Temp directory and all of its subdirectories:
You should now see the following screenshot: