Skip to content
PowerShell Commands

Copy-Item

Copy-Item [[-Destination] <String>] [-Confirm] [-Container] [-Credential <PSCredential>] [-Exclude <String[]>][-Filter <String>] [-Force] [-FromSession <PSSession>] [-Include <String[]>] -LiteralPath* <String[]> [-PassThru][-Recurse] [-ToSession <PSSession>] [-UseTransaction] [-WhatIf] [<CommonParameters>]
Copy-Item [-Path*] <String[]> [[-Destination] <String>] [-Confirm] [-Container] [-Credential <PSCredential>][-Exclude <String[]>] [-Filter <String>] [-Force] [-FromSession <PSSession>] [-Include <String[]>] [-PassThru][-Recurse] [-ToSession <PSSession>] [-UseTransaction] [-WhatIf] [<CommonParameters>]

The Copy-Item cmdlet copies an item from one location to another location in the same namespace. For instance, it can copy a file to a folder, but it cannot copy a file to a certificate drive.

This cmdlet does not cut or delete the items being copied. The particular items that the cmdlet can copy depend on the Windows PowerShell provider that exposes the item. For instance, it can copy files and directories in a file system drive and registry keys and entries in the registry drive.

This cmdlet can copy and rename items in the same command. To rename an item, enter the new name in the value of the Destination parameter. To rename an item and not copy it, use the Rename-Item cmdlet.

Parameters

-Confirm [<SwitchParameter>]

  • Default value is False
  • Accepts pipeline input False

Prompts you for confirmation before running the cmdlet.

-Container [<SwitchParameter>]

  • Default value is False
  • Accepts pipeline input False

Indicates that this cmdlet preserves container objects during the copy operation.

-Credential <PSCredential>

  • Default value is None
  • Accepts pipeline input ByPropertyName

Specifies a user account that has permission to perform this action. The default is the current user.

Type a user name, such as User01 or Domain01\User01, or enter a PSCredential object, such as one generated by the Get-Credential cmdlet. If you type a user name, you will be prompted for a password.

This parameter is not supported by any providers installed with Windows PowerShell.

-Destination <String>

  • Default value is None
  • Accepts pipeline input ByPropertyName

Specifies the path to the new location. To rename a copied item, include the new name in the value.

-Exclude <String[]>

  • Default value is None
  • Accepts pipeline input False

Specifies, as a string array, an item or items that this cmdlet excludes from the operation. Wildcards are permitted.

-Filter <String>

  • Default value is None
  • Accepts pipeline input False

Specifies a filter in the provider's format or language. The value of this parameter qualifies the Path parameter. The syntax of the filter, including the use of wildcards, depends on the provider. Filters are more efficient than other parameters, because the provider applies them when the cmdlet gets the objects, rather than have Windows PowerShell filter the objects after they are retrieved.

-Force [<SwitchParameter>]

  • Default value is False
  • Accepts pipeline input False

Indicates that this cmdlet will copy items that cannot otherwise be changed, such as copying over a read-only file or alias.

-FromSession <PSSession>

  • Default value is None
  • Accepts pipeline input False

Specifies the PSSession object from which a remote file is being copied. When you use this parameter, the Path and LiteralPath parameters refer to the local path on the remote machine.

-Include <String[]>

  • Default value is None
  • Accepts pipeline input False

Specifies, as a string array, only those items upon which the cmdlet will act, excluding all others.

-LiteralPath <String[]>

  • This value is required
  • Default value is None
  • Accepts pipeline input ByPropertyName

Specifies a path to the item. The value of the LiteralPath parameter is used exactly as it is typed. No characters are interpreted as wildcards. If the path includes escape characters, enclose it in single quotation marks. Single quotation marks tell Windows PowerShell not to interpret any characters as escape sequences.

-PassThru [<SwitchParameter>]

  • Default value is False
  • Accepts pipeline input False

Returns an object representing the item with which you are working. By default, this cmdlet does not generate any output.

-Path <String[]>

  • This value is required
  • Default value is None
  • Accepts pipeline input ByPropertyName

Specifies, as a string array, the path to the items to copy.

-Recurse [<SwitchParameter>]

  • Default value is False
  • Accepts pipeline input False

Indicates that this cmdlet performs a recursive copy.

-ToSession <PSSession>

  • Default value is None
  • Accepts pipeline input False

Specifies the PSSession object to which a remote file is being copied. When you use this parameter, the Path and LiteralPath parameters refer to the local path on the remote machine.

-UseTransaction [<SwitchParameter>]

  • Default value is False
  • Accepts pipeline input False

Includes the command in the active transaction. This parameter is valid only when a transaction is in progress.

-WhatIf [<SwitchParameter>]

  • Default value is False
  • Accepts pipeline input False

Shows what would happen if the cmdlet runs. The cmdlet is not run.

<CommonParameters>

This cmdlet supports the common parameters: Verbose, Debug,ErrorAction, ErrorVariable, WarningAction, WarningVariable,OutBuffer, PipelineVariable, and OutVariable.

Inputs
System.String
You can pipe a string that contains a path to this cmdlet.
Outputs
None or an object representing the copied item.
When you use the PassThru parameter, this cmdlet returns an object that represents the copied item. Otherwise, this cmdlet does not generate any output.
Examples
  1. Copy a file to the specified directory:
    PS C:\> Copy-Item "C:\Wabash\Logfiles\mar1604.log.txt" -Destination "C:\Presentation"
    

    This command copies the mar1604.log.txt file to the C:\Presentation directory. The command does not delete the original file.

  2. Copy the contents of a directory to another directory:
    PS C:\> Copy-Item "C:\Logfiles" -Destination "C:\Drawings" -Recurse
    

    This command copies the entire contents of the Logfiles directory into the Drawings directory. If the LogFiles 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.

  3. Copy the contents of a directory to another directory and create the destination directory if it does:
    
       not exist
    
    PS C:\> Copy-Item C:\Logfiles -Destination C:\Drawings\Logs -Recurse
    

    This command copies the contents of the C:\Logfiles directory to the C:\Drawings\Logs directory. It creates the \Logs subdirectory if it does not already exist.

  4. Copy a file to the specified directory and rename the file:
    PS C:\> Copy-Item "\\Server01\Share\Get-Widget.ps1" -Destination "\\Server12\ScriptArchive\Get-Widget.ps1.txt"
    

    This command uses the Copy-Item cmdlet to copy the Get-Widget.ps1 script from the \\Server01\Share directory to the \\Server12\ScriptArchive directory. As part of the copy operation, the command also changes the item name from Get-Widget.ps1 to Get-Widget.ps1.txt, so it can be attached to email messages.

  5. Copy a file to a remote computer:
    PS C:\> $Session = New-PSSession -ComputerName "Server01" -Credential "Contoso\PattiFul"
    PS C:\>  Copy-Item "D:\Folder001\test.log" -Destination "C:\Folder001_Copy\" -ToSession $Session
    

    The first command creates a session to the remote computer named Server01 with the credential of Contoso\PattiFul and stores the results in the variable named $Session.

    The second command uses the Copy-Item cmdlet to copy test.log from the D:\Folder001 folder to the C:\Folder001_Copy folder on the remote computer using the session information stored in the $Session variable. This command does not delete the original file.

  6. Copy the entire contents of a folder to a remote computer:
    PS C:\> $Session = New-PSSession -ComputerName "Server02" -Credential "Contoso\PattiFul"
    PS C:\>  Copy-Item "D:\Folder002\" -Destination "C:\Folder002_Copy\" -ToSession $Session
    

    The first command creates a session to the remote computer named Server01 with the credential of Contoso\PattiFul and stores the results in the variable named $Session.

    The second command uses the Copy-Item cmdlet to copy the entire contents from the D:\Folder002 folder to the C:\Folder002_Copy directory on the remote computer using the session information stored in the $Session variable. The subfolders will be copied with their file trees intact.

  7. Recursively copy the entire contents of a folder to a remote computer:
    PS C:\> $Session = New-PSSession -ComputerName "Server04" -Credential "Contoso\PattiFul"
    PS C:\>  Copy-Item "D:\Folder003\" -Destination "C:\Folder003_Copy\" -ToSession $Session -Recurse
    

    The first command creates a session to the remote computer named Server01 with the credential of Contoso\PattiFul and stores the results in the variable named $Session.

    The second command uses the Copy-Item cmdlet to copy the entire contents from the D:\Folder003 folder to the C:\Folder003_Copy directory on the remote computer using the session information stored in the $Session variable. The subfolders will be copied with their file trees intact. Since this command uses the Recurse parameter, the operation will create the Folder003_Copy folder if it does not already exist.

  8. Copy a file to a remote computer and then rename the file:
    PS C:\> $Session = New-PSSession -ComputerName "Server04" -Credential "Contoso\PattiFul"
    PS C:\>  Copy-Item "D:\Folder004\scriptingexample.ps1" -Destination "C:\Folder004_Copy\scriptingexample_copy.ps1" -ToSession $Session
    

    The first command creates a session to the remote computer named Server01 with the credential of Contoso\PattiFul and stores the results in the variable named $Session.

    The second command uses the Copy-Item cmdlet to copy scriptingexample.ps1 from the D:\Folder004 folder to the C:\Folder004_Copy folder on the remote computer using the session information stored in the $Session variable. As part of the copy operation, the command also changes the item name from scriptingexample.ps1 to scriptingexample_copy.ps1, so it can be attached to email messages. This command does not delete the original file.

  9. Copy a remote file to the local computer:
    PS C:\> $Session = New-PSSession -ComputerName "Server01" -Credential "Contoso\PattiFul"
    PS C:\>  Copy-Item "C:\MyRemoteData\test.log" -Destination "D:\MyLocalData\" -FromSession $Session
    

    The first command creates a session to the remote computer named Server01 with the credential of Contoso\PattiFul and stores the results in the variable named $Session.

    The second command uses the Copy-Item cmdlet to copy test.log from the remote C:\MyRemoteData\ to the local D:\MyLocalData folder using the session information stored in the $Session variable. This command does not delete the original file.

  10. Copy the entire contents of a remote folder to the local computer:
    PS C:\> $Session = New-PSSession -ComputerName "Server01" -Credential "Contoso\PattiFul"
    PS C:\>  Copy-Item "C:\MyRemoteData\scripts" -Destination "D:\MyLocalData\" -FromSession $Session
    

    The first command creates a session to the remote computer named Server01 with the credential of Contoso\PattiFul and stores the results in the variable named $Session.

    The second command uses the Copy-Item cmdlet to copy the entire contents from the remote C:\MyRemoteData\scripts folder to the local D:\MyLocalData folder using the session information stored in the $Session variable. If the scripts folder contains files in subfolders, those subfolders will be copied with their file trees intact.

  11. Recursively copy the entire contents of a remote folder to the local computer:
    PS C:\> $Session = New-PSSession -ComputerName "Server01" -Credential "Contoso\PattiFul"
    PS C:\>  Copy-Item "C:\MyRemoteData\scripts" -Destination "D:\MyLocalData\scripts" -FromSession $Session
    

    The first command creates a session to the remote computer named Server01 with the credential of Contoso\PattiFul and stores the results in the variable named $Session.

    The second command uses the Copy-Item cmdlet to copy the entire contents from the remote C:\MyRemoteData\scripts folder to the local D:\MyLocalData\scripts folder using the session information stored in the $Session variable. Since this command uses the Recurse parameter, the operation will create the scripts folder if it does not already exist .If the scripts folder contains files in subfolders, those subfolders will be copied with their file trees intact.

Additional Notes
 * This cmdlet is similar to the cp or copy commands in other shells.

 This cmdlet is designed to work with the data exposed by any provider. To list the providers available in your 
 session, type `Get-PsProvider`. For more information, see about_Providers.

 *

This work is licensed under a Creative Commons Attribution 4.0 International. It is attributed to Microsoft Corporation and can be found here.

PowerShell Commands