Move file to Teams SharePoint directory using Powershell - powershell

I'm trying to move a file to a Teams Channel\SharePoint directory using powershell. It appears to me that I only need three lines of code for proof of concept. I'm using the -UseWebLogin to get my login information
$Output = Get-ChildItem C:\Temp\ToSharepoint\Test.txt
Connect-PnPOnline -Url "https://My365.sharepoint.com/" –UseWebLogin
Add-pnpfile -path $Output -Folder https://My365.sharepoint.com/:f:/s/MyServicesInc/%20ITCommunications/Forms/AllItems.aspx
I've tried numerous strings for the destination but none of them seem to work. I'm currently receiving this error message,
Add-pnpfile : Access denied.
At line:1 char:1
Add-pnpfile -path $Output -Folder https://My365.sharepoint.com/:f:/ ...
+ CategoryInfo : WriteError: (:) [Add-PnPFile], ServerUnauthorizedAccessException
+ FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Files.AddFile
I am able to upload a file manually so I believe my credentials are not the issue but I don't know that for sure.
Any help would be greatly appreciated! - Thanks

Please use the Url of your teams channel site url under following code:
Connect-PnPOnline -Url
"**https://My365.sharepoint.com/sites/Team1-PrivateChannel38**"
–UseWebLogin
In addition, use a relative folder path like this:
Shared Documents/Private Channel 38/Folder1
Here is my test which works well:
$Output = Get-ChildItem C:\Temp\lulu4.jpg
Connect-PnPOnline -Url "https://tenant.sharepoint.com/sites/Team1-PrivateChannel38" –UseWebLogin
Add-pnpfile -path $Output -Folder "Shared Documents/Private Channel 38/Folder1"

Here's the interface I'm working with and what I'm using. The only way I could get the picture in was to submit this as an answer, sorry about that. I'm still not able to get it to work. I am a member of the group rather than the owner and I'm wondering if that is the issue although I can manually upload files and delete file from sharepoint.
$Output = Get-ChildItem C:\Temp\ToSharepoint\Test.txt
Connect-PnPOnline -Url "https://My365.sharepoint.com/sites/UnitedWeStand" –UseWebLogin
Add-pnpfile -path $Output -Folder "Documents/IT Communications"
Thanks again!
My Interface

Related

How to remove OneDrive folder using PowerShell

I'm trying to remove a user OneDrive folder using PowerShell but I'm not seeing any sucess even though I've been searching around internet so I would be really appreciated if I can get any help or suggestion.
so Just for testing purpose, I'm trying to delete my a folder in my own OneDrive called "Testing" and I wanted to delete everything in there including subfolders and files.
Connect-SPOService -Url https://company-admin.sharepoint.com
$OneDriveURLs = Get-SPOSite -IncludePersonalSite $true -Limit All -Filter "Url -like '-my.sharepoint.com/personal/'"
foreach($OneDriveURL in $OneDriveURLs)
{
Connect-SPOService -Url https://company-admin.sharepoint.com
Connect-PnPOnline -Url $OneDriveURLs
Remove-PnPFolder -Name "Google Drive" -Folder "Testing"
}
Your cmdlet format is not correct, you should follow the structure, more about it in Microsoft Docs
You would need to change <username> to the name the personal drive.
Change your format to :
$drive= https://company-admin.sharepoint.com/personal/<username>
$folder = 'testing'
Remove-PnPFolder -Name $drive -Folder $folder
If that does not work, as an alternative you can try the following powershell module OneDrive and use:
Remove-ODItem -AccessToken $Auth.access_token -ResourceId "https://sepagogmbh-my.sharepoint.com/" -path "/Upload"
You can read more about the module on their GitHub page.

How to move folder from local drive to Sharepoint folder using PowerShell?

My sample code:
$Creds = Get-Credential
Add-PnPStoredCredential -Name $URL -Username $Creds.UserName -Password $Creds.Password
Import-Module SharePointPnPPowershellOnline
$URL = "https://myweb.sharepoint.com/sites/abc"
connect-PnPOnline $URL
$SourceFolder = "C:\Users\myLocation"
$TargetFolder = "Shared Documents/Sample Images"
#Get-PnPList
Move-PnPFolder -Folder $SourceFolder -TargetFolder $TargetFolder
I got the following error message:
Move-PnPFolder : File Not Found.
At C:\Users\MyLocation\PowerShell\FileUpload.ps1:14 char:1
+ Move-PnPFolder -Folder $SourceFolder -TargetFolder $TargetFolder
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (:) [Move-PnPFolder], ServerException
+ FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Files.MoveFolder
I used the module SharePointPnPPowershellOnline for connect with Sharepoint.
I think you can only move a folder from one SharePoint location to another using Move-PnPFolder. It is not supported moving the folder from local. As stated here: https://learn.microsoft.com/en-us/powershell/module/sharepoint-pnp/move-pnpfolder?view=sharepoint-ps
Move a folder to another location in the current web.
Please refer to below articles to upload a folder to SharePoint using PowerShell.
SharePoint Online: Upload a Folder using PowerShell
SharePoint Online: Migrate a Folder with Sub-Folders and Files to SharePoint Online using PowerShell

Issues with PnP.Powershell Get-PnPfile

I have been getting the following error while using the cmdlet Get-PnPFile.
Get-PnPFile : The property or field 'ServerRelativeUrl' has not been
initialized. It has not been requested or the request has not been
executed. It may need to be explicitly requested. At line:1 char:1
Get-PnPFile -Url $fileURL -Path $downloadPath -Filename Leavers.CSV - ...
+ CategoryInfo : WriteError: (:) [Get-PnPFile], PropertyOrFieldNotInitializedException
+ FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Files.GetFile
The code in question is bellow:
$siteURL = "https://contoso.sharepoint.com/sites/ExampleSite"
$fileURL = "/sites/ExampleSite/Path/To/The/File.csv"
$downloadPath = "C:\Temp"
Connect-PnPOnline -Url $siteURL -Credentials (Get-Credential)
Get-PnPFile -Url $fileURL -Path $downloadPath -Filename File.CSV -AsFile
The odd part is that it has previously worked and even now seems to work intermittently but more often than not it seems to error and I haven't been able to identify a pattern.
The Connect-PnPOnline command runs without error but doesn't seem to work correctly the majority of the time. I have been using Get-PnPSite to check the connection to the SharePoint site but it doesn't seem to be populating after the connection is made as shown bellow.
PS C:\Windows\system32> Get-PNPsite
Url CompatibilityLevel
--- ------------------
On the odd occasions when the Get-PnPFile command works the two fields above are populated correctly.
I have been googling this issue and have found some other people who have had success adding a variable to the start of either the Get-PnPFile cmdlet or the Connect-PnPOnline cmdlet Like so:
$connection = Connect-PnPOnline -Url $siteURL -Credentials (Get-Credential)
$file = Get-PnPFile -Url $fileURL -Path $downloadPath -Filename File.CSV -AsFile
But this hasn't worked for me. I have also tried down and upgrading the version of PnP.Powershell From 1.8.0 to 1.8.11 and back down to 1.7.0 but this also hasn't had any effect.
Hope someone out there has some answers! Thanks in advance

add-pnpfile between powershell

I got this inconvenience, im trying to copy a file between sharepoint, I already have the file in a variable but when I tried, it said:
Add-PnPFile : Access denied. You do not have permission to perform this
action or access this resource.
At line:1 char:1
+ Add-PnPFile -Path "http://domain/$cow" -Folder Documents
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (:) [Add-PnPFile],
ServerUnauthorizedAccessException
+ FullyQualifiedErrorId :
EXCEPTION,SharePointPnP.PowerShell.Commands.Files.AddFile
I would like to know how i can open two connection, one to grab the file in that sharepoint and then send to the other one.
this is the code I'm using
Add-PnPFile -Path "http://domain/$cow" -Folder Documents
$cow is variable that contiain my file and Document is where i wanna add the file.
Thank you
you have to connect to the first SharePoint first with:
Connect-PnPOnline –Url https://yoursite.sharepoint.com –Credentials (Get-Credential)
next get the file you want with:
Get-PnPFile -Url Documents/sample.doc -Path c:\temp -FileName sample.doc -AsFile
and finally, after connecting to the second SharePoint, add the file with:
Add-PnPFile -Path c:\temp\sample.doc -Folder "Documents"

I need to remove folders by a specific name, if it exist

I have a file server where the users have their own diskspace. And I need to delete a certain folder on each users diskspace, but not all users have this folder. Also the users are divided into department, so the layout of the folders looks like this:
D:\users\departmentA\usernameA\foldertodelete
D:\users\departmentA\usernameB\foldertodelete
D:\users\departmentB\usernameC\foldertodelete
D:\users\departmentC\usernameD\foldertodelete
...
How can I make this happen? Im thinking of using Poweshell, and I have been reading about Test-Path, but I dont know how to work with this then the path is different for all the users.
This is fairly straightforward with PowerShell:
$TargetName = "foldertodelete"
$BaseDir = "D:\Users"
# Iterate over each department directory
foreach($Department in #(Get-ChildItem $BaseDir -Directory)){
# Within each department, iterate over each user directory
foreach($User in #(Get-ChildItem $Department -Directory)){
# Check if the undesirable folder exists
if([System.IO.Directory]::Exists(($TargetPath = Join-Path -Path $User -ChildPath $TargetName))){
# If so, remove the directory and all child items
Remove-Item $TargetPath -Recurse
}
}
}
The -Directory parameter on Get-ChildItem is available in PowerShell 3.0 and above. For PowerShell 2.0 you could use:
Get-ChildItem $BaseDir |Where {$_.PSIsContainer}
Thank you for your answer. I do have some problem making the script work. It looks like the script don't understand the BaseDir part. The department folder where the users have their folder is located on D:\Users. I copied the script to C:\skript. And the error message show that it is looking for the userfolder on my C drive. It should look on D:\users\AAL-users\username
Here is the error message:
PS C:\skript> .\RemoveFolder.ps1
Get-ChildItem : Cannot find path 'C:\skript\AAL-users' because it does not exist.
At C:\skript\RemoveFolder.ps1:7 char:22
+ foreach($User in #(Get-ChildItem $Department -Directory)){
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\skript\AAL-users:String) [Get-ChildItem], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
I did try to copy the script to my D:\users folder. This time I got no error messages, but the script did not delete any folders as it is suposed to.
Try the below...
Remove-Item -path D:\Users\* -Filter *specificnameof the folder* -WhatIf --(Whatif allows you to test a command before you run)
Remove whatif and execute the delete
Remove-Item -path C:\users\* -Filter *specificnameof the folder*
Here is the exact code I run now:
PS D:> Remove-Item -path "D:\DU Users*" -Filter notes85
PS D:>
No error messages, but also, the folder in question is not removed.