spot_img

PowerShell Copy-Item | PowerShell Copy file

The Copy-Item cmdlet copies an item form one location to another location within a namespace. For instance, this cmdlet can copy a file to a folder, but it cannot copy a file to a certificate drive. It does not delete or cut the items being copied. Those specific items that the cmdlet copy depend on the PowerShell provider, which exposes that item.

For instance, this cmdlet can copy files and directories in a file system drive and registry keys and entries in the registry drive. It can copy and rename the items in the same cmdlet. Enter the new name in the value of -Destination parameter to rename an item.

Syntax

Parameters

-Confirm

The -Confirm parameter prompts a confirmation before running the cmdlet.

-Path

The string array in the -Path parameter is used to specify a path to the items to copy. Wildcard characters are accepted.

-LiteralPath

The -LiteralPath parameter is used to specify a path of a location. Its value is used exactly as it is typed. If the path includes the escape characters, enclose it in single quotation marks. Single quotation mark tells the Windows PowerShell that it should not interpret any character as an escape sequence. There is no character in the cmdlet, which is interpreted as a wildcard.

-Destination

The -Destination parameter is used to specify the path to the new location. The default is the current location. No wildcards are accepted. To rename the item being copied, given a new name in the value of -Destination parameter.

-Filter

The -Filter parameter specifies a filter to qualify the -Path parameter. The FileSystem provider is the only PowerShell provider that supports the uses of filters. This parameter is more efficient as the provider applies the filters when the cmdlet gets the object, rather than having Powershell filters the object after they’re accessed.

-Force

This parameter indicates that this cmdlet copies those items that cannot be changed, such as copying over read-only files or aliases.

-Container

The -Container parameter indicates that this cmdlet preserves the container objects during the copy operation. By default, this parameter is set to True.

-Include

The items that this cmdlet includes in the operation are specified as a string array. The value of -Include parameter qualifies the -Path parameter. Enter a pattern or a path element, such as *.txt. Wildcard characters are accepted. The -Exclude parameter is effective only when the cmdlet includes the contents of an item, such as C:*, the wildcard character ‘*’ is used to specify the contents of the C: directory.

-Exclude

The items that this cmdlet excludes in operation are specified as a string array. The value of -Exclude parameter qualifies the -Path parameter. Enter a pattern or a path element, such as *.txt. Wildcard characters are accepted. The -Exclude parameter is effective only when the cmdlet includes the contents of an item, such as C:*, the wildcard character ‘*’ is used to specify the contents of the C: directory.

-Recurse

The -Recurse parameter indicates that this cmdlet does a recursive copy.

-PassThru

The -PassThru parameter returns an object that represents an item with which we are working. Be default; it does not generate any output.

-Credential

The -Credential parameter is not supported by any provider installed with the PowerShell. By default, the current user is used in this parameter.

-WhatIf

The -WhatIf parameter displays what would happen if the cmdlet executes. The cmdlet does not execute.

-FromSession

The -FromSession parameter is used to specify the PSSession object from which a remote file is being copied. When we use it, both -path and -LiteralPath parameters refer to a local path on the remote machine.

-ToSession

The -ToSession parameter is used to specify the PSSession object to which a remote file is being copied. When we use it, the -Destination parameter refers to a local path on the remote machine.

Examples

Example 1: Copy a file to a specified location

PowerShell Copy-Item

The cmdlet in this example copies the powershell.txt file to the D:windows directory. The original file is not deleted.

Example 2: Copy the content of a directory to a new directory

PowerShell Copy-Item

The cmdlet in this example copies the content of C:tutoraspire source directory and creates a new destination directory. The new destination directory, files, is created in a D: drive.

Example 3: Copy the content of a directory to an existing directory

PowerShell Copy-Item

The cmdlet in this example copies the content of the C:tutoraspiredirectory into the existing Directory C:PowerShelldirectory. The tutoraspire directory is not copied.

If the tutoraspire directory contains the files in subdirectories, they are also copied with their file trees intact. By default, the

The -Container parameter is set to True, which preserves the structure of the directory.

Example 4: Copy the file to a given directory and rename that file

PowerShell Copy-Item

The cmdlet in this example copy the file s.txtfrom the D:directory to the D:jtp directory and change the file name from s.txt to s1.txt.


spot_img
Previous articleLearn MS Word Tutorial
Next articleWhat is PowerShell cmdlet