| | 0

Updated: Use PowerShell Module OneDrive from PowerShellGallery to work with OneDrive via command line

21wrote a PowerShell module to work with OneDrive via command line. The module use the documented OneDrive web API from Microsoft (https://dev.onedrive.com/README.htm). If you already have the module PowerShellGet to access PowerShellGallery installed, you can easily download this module with a single command:

Install-Module -Name OneDrive -Scope CurrentUser

You can update the module to a newer version with the same command (add -force). If you don‘t use PowerShellGet currently, go to the Gallery on https://www.powershellgallery.com/packages/OneDrive and click on “Get Started”.

If you have any questions or feature requests, write me an email or get in touch with me via www.twitter.com/MarcelMeurer

Features (version 0.9.2 or higher)

The actual version of this module supports:

  • Authentication with OneDrive (half web-based)
  • Get properties of folder and files
  • List folders and files
  • Search files and folders
  • Create folders
  • Delete folders and files
  • Download files
  • Upload files (up to 100 Mbyte)

Next features to be implemented:

  • Rename files and folders
  • Check if an unattended login for authentication without a web form is possible

How to use the module (examples)

The functions accept a wider range of parameters as used in these examples. To display all parameters, examples and a description use “get-help”:

get-help Get-ODChildItems -detailed

To display all functions of this module type:

Get-Command -Module OneDrive

You can address files and folders through their path (-path) or element id (-ElementID).

Authentication

For each interaction with the OneDrive API you need an access token. You can generate a fresh token for reading and writing access (this lasts for 1 hour) with:

$Authentication=Get-ODAuthentication -ClientID “00000000……..”

$AuthToken=$Authentication.access_token

Get-ODAuthentication opens a browser window to authenticate yourself with your live id and to accept access through this module.

You can get your own client id on https://apps.dev.microsoft.com

Update: Make sure that you generate a client id in the section ‘Converged applications’ for the token-based authentication and select “Allow implicit flow”. Here is a short video: https://youtu.be/oyILyPQ066E

List all OneDrives available to your account (normally only one)

1

2

3

4

5

6

7

Get-ODDrives -AccessToken $AuthToken

 

id               driveType owner    quota

 

—               ——— —–    —–

 

8badcff017eaa324 personal  @{user=} @{deleted=21419952; remaining=22945965909; state=normal; total=42949672960; used…

 

List folders and files in your OneDrive root:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

Get-ODChildItems -AccessToken $AuthToken -Path “/” | ft

 

name                 size lastModifiedDateTime     id                     folder

 

—-                 —- ——————–     —                     ——

 

Backups        1377598479 2015-10-30T19:24:08.84Z  8BADCFF017EAA324!4036  @{childCount=2}

 

Bilder            1503567 2016-02-19T20:28:03.243Z 8BADCFF017EAA324!12091 @{childCount=2}

 

Data                    0 2016-02-21T12:17:54.26Z  8BADCFF017EAA324!12158 @{childCount=4}

 

Daten               24606 2015-12-07T09:03:17.877Z 8BADCFF017EAA324!4034  @{childCount=1}

 

Dokumente       968612599 2016-02-19T10:25:43.923Z 8BADCFF017EAA324!129   @{childCount=4}

 

Eigene Bilder 14963858114 2016-02-19T20:29:37.057Z 8BADCFF017EAA324!1205  @{childCount=42}

 

Musik                   0 2015-10-01T12:00:24.467Z 8BADCFF017EAA324!11786 @{childCount=0}

 

Videos         2689233875 2015-11-04T11:33:21.09Z  8BADCFF017EAA324!9602  @{childCount=1}

 

demo.xls          2873344 2016-02-19T20:57:18.08Z  8BADCFF017EAA324!12156

 

Notes.txt            2467 2016-02-15T20:01:15.1Z   8BADCFF017EAA324!12102

List folders and files in the folder “/data”

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

Get-ODChildItems -AccessToken $AuthToken -Path “/data” | ft

 

name      size lastModifiedDateTime     id                     folder

 

—-      —- ——————–     —                     ——

 

backup       0 2016-02-21T12:17:11.64Z  8BADCFF017EAA324!12160 @{childCount=0}

 

documents    0 2016-02-21T12:17:54.26Z  8BADCFF017EAA324!12161 @{childCount=2}

 

log files    0 2016-02-21T12:17:02.043Z 8BADCFF017EAA324!12159 @{childCount=0}

 

pictures     0 2016-02-21T12:17:36.97Z  8BADCFF017EAA324!12162 @{childCount=0}

 

List folders and files in the folder “data” by element id:

 

Get-ODChildItems -AccessToken $AuthToken –ElementID 8BADCFF017EAA324!12158

 

(same as above)

Create a new folder “2016” under “/data/documents”

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

New-ODFolder -AccessToken $AuthToken -Path “/data/documents” -FolderName “2016”

 

@odata.context       : https://api.onedrive.com/v1.0/$metadata#drives(‘me’)/items(‘root%252Fdata%252Fdocuments’)/childr

 

en/$entity

 

createdBy            : @{application=; user=}

 

createdDateTime      : 2016-02-21T12:36:22.337Z

 

cTag                 : adDo4QkFEQ0ZGMDE3RUFBMzI0ITEyMTY1LjYzNTkxNjU0OTgyMzM3MDAwMA

 

eTag                 : aOEJBRENGRjAxN0VBQTMyNCExMjE2NS4w

 

id                   : 8BADCFF017EAA324!12165

 

lastModifiedBy       : @{application=; user=}

 

lastModifiedDateTime : 2016-02-21T12:36:22.337Z

 

name                 : 2016

 

parentReference      : @{driveId=8badcff017eaa324; id=8BADCFF017EAA324!12161; path=/drive/root:/Data/documents}

 

size                 : 0

 

webUrl               : https://onedrive.live.com/redir?resid=8BADCFF017EAA324!12165

 

fileSystemInfo       : @{createdDateTime=2016-02-21T12:36:22.337Z; lastModifiedDateTime=2016-02-21T12:36:22.337Z}

 

folder               : @{childCount=0}

Upload a file to OneDrive “/data/documents/2016”

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

Add-ODItem -AccessToken $AuthToken -Path “/Data/documents/2016” -LocalFile “AzureML with PowerShell.docx”

 

StatusCode        : 201

 

StatusDescription : Created

 

Content           : {“@odata.context”:”https://api.onedrive.com/v1.0/$metadata#drives(‘me’)/items/$entity”,”@content.do

 

wnloadUrl”:”https://public.dm2302.livefilestore.com/y3mNucsMgwxhhk3DbsU_DqpD7wo5LD_0Un3dfNzMPrPI3tQ

 

D4…

 

RawContent        : HTTP/1.1 201 Created

 

Content-Location: https://mczd5q.dm2302.livefilestore.com/y3p5-QWu8f3PAjkT2zaXz-j4h_viQ6QeiYcItUYJb

 

yaWf6jDcUzohA4FnsRHqzAgJQIasPyCrezR0fzN4Ip3qiekBXFkcH3ThDCO_0hJwgdGVcBWidznZfE-…

 

Forms             : {}

 

Headers           : {[Content-Location, https://mczd5q.dm2302.livefilestore.com/y3p5-QWu8f3PAjkT2zaXz-j4h_viQ6QeiYcItUY

 

JbyaWf6jDcUzohA4FnsRHqzAgJQIasPyCrezR0fzN4Ip3qiekBXFkcH3ThDCO_0hJwgdGVcBWidznZfE-lx0jz7t18ZN_IrZ55b

 

mahWs7DF7GrMEtOV3DPTg_3WwQFU3k601mW_3YfokTBZBmTcyAqe7p9D6], [X-WLSPROXY, BN1301____PAP100],

 

[X-MSNSERVER, DM2302____PAP236], [Strict-Transport-Security, max-age=31536000;

 

includeSubDomains]…}

 

Images            : {}

 

InputFields       : {}

 

Links             : {}

 

ParsedHtml        : mshtml.HTMLDocumentClass

 

RawContentLength  : 1435

Get some metadata of a file or folder

1

2

3

4

5

6

7

8

9

10

11

12

13

Get-ODItemProperty -AccessToken $AuthToken -Path “/Data/documents/2016/AzureML with PowerShell.docx”

 

@odata.context       : https://api.onedrive.com/v1.0/$metadata#drives(‘me’)/items(name,size,lastModifiedDateTime,id,fol

 

der)/$entity

 

name                 : AzureML with PowerShell.docx

 

size                 : 110804

 

lastModifiedDateTime : 2016-02-21T13:07:41.11Z

 

id                   : 8BADCFF017EAA324!12169

Get all metadata of a file or folder by element id (“” select all properties)
1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

Get-ODItemProperty -AccessToken $AuthToken -ElementID 8BADCFF017EAA324!12169 -SelectProperties “”

 

@odata.context       : https://api.onedrive.com/v1.0/$metadata#drives(‘me’)/items/$entity

 

@content.downloadUrl : https://public.dm2302.livefilestore.com/y3m3fRaVdsz2KgyxKlgGlK6CO744YSaXyk0llE9uhvg96DFAgjdjkjBL

 

cULS4_Ddh4xcBrp8HvTCmMZsAtXQrOPBPxUD2hYH-PSUoEs-g5foUs5AzcwjCp3s8jo6VCBAGDGD64q2PsgspT-vXlT4ppxd

 

T7GzvU2yrIT5NUs4Poj9b_KlA7_TZ2-SYfffKJEqZNh

 

createdBy            : @{application=; user=}

 

createdDateTime      : 2016-02-21T13:07:41.11Z

 

cTag                 : aYzo4QkFEQ0ZGMDE3RUFBMzI0ITEyMTY5LjI1Nw

 

eTag                 : aOEJBRENGRjAxN0VBQTMyNCExMjE2OS4z

 

id                   : 8BADCFF017EAA324!12169

 

lastModifiedBy       : @{application=; user=}

 

lastModifiedDateTime : 2016-02-21T13:09:32.287Z

 

name                 : AzureML with PowerShell.docx

 

parentReference      : @{driveId=8badcff017eaa324; id=8BADCFF017EAA324!12165; path=/drive/root:/Data/documents/2016}

 

size                 : 110804

 

webUrl               : https://onedrive.live.com/redir?resid=8BADCFF017EAA324!12169

 

file                 : @{hashes=; mimeType=application/vnd.ms-word.document.12}

 

fileSystemInfo       : @{createdDateTime=2016-02-21T13:07:41.11Z; lastModifiedDateTime=2016-02-21T13:07:41.11Z}

Download a file from OneDrive

1

2

3

Get-ODItem -AccessToken $AuthToken -Path “/Data/documents/2016/Powershell array custom objects.docx”

 

0

If “0” returnes, the download was successful

Delete an item

1

2

3

Remove-ODItem -AccessToken $AuthToken -Path “/Data/documents/2016/Azure-big-picture.old.docx”

 

0

If “0” returnes, the download was successful

Search for items in a sub folder recursively

Take a look at OneDrive API documentation to see how the search (preview) works (file and folder names, in file search, …)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

Search-ODItems -AccessToken $AuthToken -Path “/My pictures” -SearchText “FolderA” | ft

 

name       size lastModifiedDateTime     id                     folder

 

—-       —- ——————–     —                     ——

 

FolderA       0 2016-02-18T15:03:28.637Z 8BADCFF017EAA324!12106 @{childCount=0}

 

FolderA 10    0 2016-02-18T15:05:28.94Z  8BADCFF017EAA324!12116 @{childCount=0}

 

FolderA 1     0 2016-02-18T15:03:43.337Z 8BADCFF017EAA324!12107 @{childCount=0}

 

FolderA 3     0 2016-02-18T15:03:50.28Z  8BADCFF017EAA324!12109 @{childCount=0}

 

FolderA 8     0 2016-02-18T15:05:02.11Z  8BADCFF017EAA324!12114 @{childCount=0}

 

FolderA 7     0 2016-02-18T15:04:58.11Z  8BADCFF017EAA324!12113 @{childCount=0}

 

FolderA 4     0 2016-02-18T15:04:40.04Z  8BADCFF017EAA324!12110 @{childCount=0}

 

FolderA 13    0 2016-02-18T15:05:36.23Z  8BADCFF017EAA324!12119 @{childCount=0}

 

FolderA 12    0 2016-02-18T15:05:33.76Z  8BADCFF017EAA324!12118 @{childCount=0}

 

FolderA 14    0 2016-02-18T15:05:45.253Z 8BADCFF017EAA324!12120 @{childCount=0}

 

FolderA 2     0 2016-02-18T15:03:45.927Z 8BADCFF017EAA324!12108 @{childCount=0}

 

FolderA 9     0 2016-02-18T15:05:09.72Z  8BADCFF017EAA324!12115 @{childCount=0}

 

FolderA 6     0 2016-02-18T15:04:46.03Z  8BADCFF017EAA324!12112 @{childCount=0}

 

FolderA 11    0 2016-02-18T15:05:31.397Z 8BADCFF017EAA324!12117 @{childCount=0}

 

FolderA 5     0 2016-02-18T15:04:43.11Z  8BADCFF017EAA324!12111 @{childCount=0}

 

Delete all files and folders found with Search-ODItems (be careful)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

foreach ($element in Search-ODItems -AccessToken $AuthToken -Path “/My pictures” -SearchText “FolderA”) {Remove-ODItem -AccessToken $AuthToken -ElementID $element.id}

 

0

 

0

 

0

 

0

 

0

 

0

 

0

 

0

 

0

 

0

 

0

 

0

 

0

 

0

 

0

Download all files found with Search-ODItems

1

2

3

4

5

6

7

foreach ($element in Search-ODItems -AccessToken $AuthToken -Path “/My pictures” -SearchText “Marcel”) {Remove-ODItem -AccessToken $AuthToken -ElementID $element.id}

 

0

 

0

 

0

 

Available functions

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

CommandType     Name                                               Version    Source

 

———–     —-                                               ——-    ——

 

(internal)        Format-ODPathorIDString                            0.9.2      OneDrive

 

Function        Get-ODAuthentication                               0.9.2      OneDrive

 

Function        Get-ODChildItems                                   0.9.2      OneDrive

 

Function        Get-ODDrives                                       0.9.2      OneDrive

 

Function        Get-ODItem                                         0.9.2      OneDrive

 

Function        Get-ODItemProperty                                 0.9.2      OneDrive

 

(internal)        Get-ODWebContent                                   0.9.2      OneDrive

 

Function        New-ODFolder                                       0.9.2      OneDrive

 

Function        Add-ODItem                                         0.9.2      OneDrive

 

Function        Remove-ODItem                                      0.9.2      OneDrive

 

Function        Search-ODItems                                     0.9.2      OneDrive