import Global from '../_global.mdx';
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

# file copy

Copies a file to another location using the Microsoft Graph

## Usage

```sh
m365 file copy [options]
```

## Options

```md definition-list
`-u, --webUrl <webUrl>`
: The URL of the site where the file is located.

`-s, --sourceUrl <sourceUrl>`
: Server-relative or absolute URL of the file.

`-t, --targetUrl <targetUrl>`
: Server-relative or absolute URL of the location.

`--newName [newName]`
: New name of the destination file.

`--nameConflictBehavior [nameConflictBehavior]`
: Behavior when a document with the same name is already present at the destination. Possible values: `fail`, `replace`, `rename`. Default is `fail`.
```

<Global />

## Permissions

<Tabs>
  <TabItem value="Delegated">

  | Resource        | Permissions         |
  |-----------------|---------------------|
  | Microsoft Graph | Sites.ReadWrite.All |

  </TabItem>
  <TabItem value="Application">

  | Resource        | Permissions         |
  |-----------------|---------------------|
  | Microsoft Graph | Sites.ReadWrite.All |

  </TabItem>
</Tabs>

## Examples

Copy a file by server-relative URL to a document library in another site collection with server relative URL

```sh
m365 file copy --webUrl https://contoso.sharepoint.com/sites/project --sourceUrl "/sites/project/Shared Documents/Document.pdf" --targetUrl "/sites/IT/Shared Documents"
```

Copy a file by absolute URL to a document library in another site collection with absolute URL

```sh
m365 file copy --webUrl https://contoso.sharepoint.com --sourceUrl "https://contoso.sharepoint.com/Shared Documents/Document.pdf" --targetUrl "https://contoso.sharepoint.com/sites/IT/Shared Documents"
```

Copy a file to a document library in another site collection and rename the file

```sh
m365 file copy --webUrl https://contoso.sharepoint.com --sourceUrl "/Shared Documents/Document.pdf" --targetUrl "/sites/IT/Shared Documents" --newName "newName"
```

Copy a file to a document library in another site collection and rename the file if a file with the same name already exists.

```sh
m365 file copy --webUrl https://contoso.sharepoint.com --sourceUrl "/Shared Documents/Document.pdf" --targetUrl "/sites/IT/Shared Documents" --nameConflictBehavior rename
```

Copy a file to Onedrive for business 

```sh
m365 file copy --webUrl https://contoso.sharepoint.com --sourceUrl "/Shared Documents/Document.pdf" --targetUrl "https://contoso-my.sharepoint.com/personal/john_contoso_onmicrosoft_com/documents"
```

## Response

The command won't return a response on success.
