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.
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.
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
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.
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
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.
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.
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
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
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.
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
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.
I think that gives us the two things that we need.
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
So lets see our target Library, if it copied a file successfully.
Now as a filed copied to target library. So now what we could do is we could remove a file from source Libray.
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
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.
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 Multiple files
Here we need to use For each object for all items in the library.
The code will be as below.
Congrats, You moved all files, Lets see resoults in Source Docs library.
SPFx Interview Questions
Now will see a Target Library, Every thing must be copied over there.
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
7 thoughts on “Move SharePoint Online Files with PowerShell”
Very good article
very good post
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?