Skip to content
Menu
SharePoint Gems
  • Power Platform
  • Development
    • SharePoint 2013
  • Migration
  • Administration
  • SP Online
  • Contact
  • About
    • Privacy Policy
SharePoint Gems

Move SharePoint Online Files with PowerShell

Posted on February 28, 2020September 29, 2021

In my last article, we learned How to install PnP Powershell on the computer. now we’ll see Move SharePoint Online Files with PowerShell.

In today’s article will discuss how to manage SharePoint online document libraries using PowerShell. we’re only moving files, copying files, removing files and telling us from different bits and pieces you know along the way.
also will see How to move sharepoint online files with Powershell.

Today we’re going to talk about managing SharePoint online documents using Powershell. so we look at using the patterns and practices teams Powershell that’s going to give us some different capabilities that we don’t have with the natives office 365  PowerShell. . in this article today we are going to create a PnP Powershell to move files from one library to another.

Over you guys to see and you can see I’ve got just a  normal document library called Docs and we’ve got three or five files here and so we’re going to is relook at how to move some of those, also you notice over here I’ve got a special column called color.

Move SharePoint Online Files with PowerShell
Contents hide
1 Open PowerShell Window
2 Move SharePoint Online Files with PowerShell
2.1 Connect PnpOnline
2.2 Start-Transcript
2.3 Get-Module version
2.4 Get-PnpListItem
2.5 Get-Member
2.6 Get-Source file
2.7 Set Target Varible
2.8 Copy-pnpFile
2.9 Remove-PnpFile
2.10 Move-PnPFile
2.11 Move Multiple files
3 SPFx Interview Questions
3.1 Share this:
3.2 Like this:
3.3 Related

Open PowerShell Window

we’ll say start and we’ll type in power and then we’re going to right-click on Windows PowerShell and say run as administrator.

Move SharePoint Online Files with PowerShell

you’ll get the UAC pop up I’ll say yes to that and just like that, we have our  PowerShell window.

PnP PowerShell to get all Site Collections

Move SharePoint Online Files with PowerShell

Connect PnpOnline

now we’re going to be using the patterns and practices  PowerShell, so to do that right the way we would start with going to log in first. So there’s my login line. connect-PNPonline -URL https://<your site>.sharepoint.com -credentials O365 we’ll hit enter now if you’re not familiar with the credentials that are doing is actually my username and password are stored in the windows credential manager and so I can just reference them by the name I set which I  call you to know 365 to make it easy.

connect-pnponline powershell script
connect-pnponline

Start-Transcript

Other thing that you guys  will want to make sure you do before you  start to need it is right as you should  always start with start-transcript and I  can give you a running log of anything

Get-Module version

So speaking of the PowerShell modules rights with PNP stuff  if I do get-module real quick you will  see that I am using the latest and  greatest of the SharePoint Online stuff  so 2.12.17. Something so keep that in  mind also if you’ve had the PNP  PowerShell on for awhile, you might need  to update to get the latest version such  a command I’ll start working exactly the  same way as mine or  you can update the module. Get-pnpmodule

Get-PnpListItem

The first thing that we  want to do is we’re going to do a get-PnPListItem -List "docs" and then box let’s name the document library and  so after five or six second you can  see that it returns five files the ID  right there a difference.

Get-pnpListItem

Get-Member

We actually dive into the script so we’re going to do is a mass. a  dollar sign ($) Cow because I know I like. and then we’re going to do a get-PNPlistitem as well and then we do – ID  and one three can grab the first one I  don’t really care what I have right now. get-pnplistitem -ID 1 -List Docs

get-listitem in variable

Now I just want to get some, that I can show you guys some of the info. so $Cow and so we can see that we’ve returned to one  item and you could have fixed this line. I’m going to do a non pipe it over to  get member right so get member who shows  us all the methods and properties of  that particular item. $cow | Get-Member

get-member

so after poking the bear a little bit, what I found was it if I do $cow field  values and hit enter that’s return ther property and suing that property’s  a bunch of child properties. So here you can see things like file leaf  rep the file name we’re going to need  that a few minutes, type of file if you  needed to know the extension,  there’s my color column as red here is  the file reference. The other the actual  just the folder structure ten so this is  important note because there’s you guys  work on your own projects right.

Get-Source file

So what I want to do now is,  I’m going to say $source  equals and then we’re going to dollar  sign cow dot field values right in tax  equates to work dot file ref so just  like that and then now if we type in  dollar sign source you can see we’ve got  that particular value stored and  variable pretty cool stuff. $source = $cow.FieldValues.Fileref

Move SharePoint Online Files with PowerShell

Set Target Varible

We’re also gonna  create a variable here called target. Because  that’s going to be where we  to send a file. So for that one I’m going  to send it to a library named Savannah  and you notice that I’ve got the the slashes (/) on both sides of the word  Savannah the reason for that is you’ll  see in just a second the string that  it’s expecting is going to require it  will be in that format so in my variable.

Move SharePoint Online Files with PowerShell

I  think that gives us the two things that  we need.

Copy-pnpFile

What we’re going to do then, is we’re going to type in copy PNPfile  and for this you want to use –  source URL and then for that we’re going  to plug in our variable source and then  for we do a – target URL and for that  one we’re going to use a variable of  target.

Copy-PnpFile -SourceUrl $Source -TargetUrl $Target
Move SharePoint Online Files with PowerShell

So lets see our target Library, if it copied a file successfully.Copy Results in target Library

Now as a filed copied to target library. So now what we could do is we could remove a file from source Libray.

Remove-PnpFile

One thing to keep in  mind is remove PNP final does not put things in the recycle bin so there’s no way to undelete or recover that file. I just removed  with PowerShell. so kind of keep that in mind it can be a little bit of scary to  manlet. Remove-PnpFile -serverRelativeUrl $Source

Move SharePoint Online Files with PowerShell

And Yes That’s it. We moved a file successfully. But there is a another mehod as well to move file. Which will move your files along with metadata.

Move-PnPFile

By using this method we are not require to use remove files, Lets see what exactly it does.

Move-PnpFile -ServerRelativeUrl $Source -TargetUrl $Target

Move SharePoint Online Files with PowerShell

Move Multiple files

Here we need to use For each object for all items in the library.
The code will be as below.

Move SharePoint Online Files with PowerShell

Congrats, You moved all files, Lets see resoults in Source Docs library.

SPFx Interview Questions

Move SharePoint Online Files with PowerShell

Now will see a Target Library, Every thing must be copied over there.

Move SharePoint Online Files with PowerShell

Hey you done it. That’s the way we can move files from one library to another. Shane created a very good video here about moving files, if you want to see then please click here. By using this article I khoe you must learned how to move sharepoint online files with powershell.

If yopu want to see more articles of this types Please visit SharePointGems

Share this:

  • Print
  • Twitter
  • Facebook
  • LinkedIn
  • WhatsApp
  • Telegram
  • Pinterest
  • Reddit

Like this:

Like Loading...

Related

7 thoughts on “Move SharePoint Online Files with PowerShell”

  1. Pingback: Create a Communication Site using PowerShell in SPO- SharePoint Gems
  2. farlin says:
    March 10, 2020 at 12:56 AM

    Very good article

    Loading...
    Reply
  3. Pingback: Programmatically Upload a File in SharePoint Document Library.
  4. Rubel says:
    March 10, 2020 at 8:56 PM

    very good post

    Loading...
    Reply
  5. Pingback: Solved: Move is not Supported on Destination Library that has smaller version limit than source
  6. best cbd gummies says:
    July 18, 2020 at 9:43 PM

    I’m the proprietor of JustCBD Store company (justcbdstore.com) and am seeking to expand my wholesale side of company. I am hoping anybody at targetdomain is able to provide some guidance . I considered that the very best way to accomplish this would be to talk to vape shops and cbd stores. I was really hoping if anyone could suggest a trustworthy web-site where I can get CBD Shops B2B Companies I am already examining creativebeartech.com, theeliquidboutique.co.uk and wowitloveithaveit.com. On the fence which one would be the very best option and would appreciate any assistance on this. Or would it be easier for me to scrape my own leads? Ideas?

    Loading...
    Reply
  7. Pingback: Retrieve all users from Site Collection using PowerShell - SharePoint Gems

Leave a ReplyCancel reply

  • Development
  • Migration
  • Poweer Apps
  • Power Automate
  • Power Platform
  • SharePoint 2013
  • SharePoint Administration
  • SharePoint Online
  • Tips
  • Uncategorized

Best of Computers

Blog Stats

  • 67,509 hits

Subscribe

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 1,433 other subscribers
Buy Me Coffee
©2025 SharePoint Gems | Powered by WordPress and Superb Themes!
%d