How to remove OneDrive folder using PowerShell - 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.

Related

How to transfer files from remote server to SharePoint Shared Documents folder

I am by no means a programmer, but I was approached with the task of moving files from a specific date range from a remote server to a SharePoint site. Currently the script that I've been working on just displays the files within the specific date range, but doesn't copy those files from the remote server to the SharePoint site. If anyone can take a look at this and let me know what I'm missing I would greatly appreciate it!
$URL = "designated url"
#First time tasks
#Install-Module SharePointPnPPowerShellOnline
$Creds = get-credential
Add-PnPStoredCredential -Name $URL -Username $Creds.Username -Password $Creds.Password
import-module SharePointPnPPowerShellOnline -Verbose
Connect-PnPOnline $URL -UseWebLogin
#Enter the name of the folder at the end of PAMInvoices
$Files = Get-ChildItem "filepath"
if ($Files = {$_.lastwritetime -gt ‘1/1/11’ -AND $_.lastwritetime -lt ‘1/1/15’})
{foreach($File in $Files)
{#$File = $Files[0]
Add-PnPFile -Folder $URL -Path $File.FullName}}

Move file to Teams SharePoint directory using 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

How do I change retention label into sharepoint with powershell with script for all the files in a specific site

I need to change the retention label in a specific site into the sharepoint. I took the following script and I edited it to suit my needs, with some small changes.
It is working but currently it applies the retention only to the folders and subfolders. Is there any way to modify the script in order to apply the retention label to the files in the folders as well?
Connect-PnPOnline -Url = https://<tenant>-my.sharepoint.com/
Connect-PnPOnline -Url SiteUrl
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$web = $Context.Web
$Context.Load($Web)
Set-PnPLabel -List "Documents" -Label "My Label"
$Folder = Get-PnPFolder -Url "Documents/My Shared Documents"
$Folder.ListItemAllFields.SetComplianceTagWithNoHold("My Label")
Invoke-PnPQuery`
I do not have access to sharepoint to test but try adding -SyncToItems $true to the Set-PnPLabel line. Have a look here for exact usage Set-PnPLabel
Set-PnPListItem -Identity $Item -ClearLabel worked when I needed to clear labels from OneDrive files.

Is there a way to update the "Created By" and "Created" fields on a SharePoint Folder using PNP

I'm migrating a folder from my local drive to SharePoint Library, and i want to be able to modify the created by fields using powershell. is this possible?
if not is there another alternative in which this can be done?
I am able to do this with add-pnpfile for files but not for folders. something in the nature of
$object = #{}
$object.Add("Modified",'1/1/2019')
$object.Add("Editor" ,"jennie.Doe#email.com")
$object.Add("Author" ,"Mac.walkers#email.com")
Add-PnPFile -Path 'C:/users/Newfolder/test.docx' -Folder 'Test/NewFolder' -Values $object
where $object is the object holding the properties and values i need to update
Sample demo:
Key point is getting the folder item and then updating.
Connect-PnPOnline -Url $siteURL -Credentials $PSCredentials
$user=Get-PnPUser | ? Email -eq "user#tenant.onmicrosoft.com"
$folderName="NewFolder"
$library=Get-PnPList -Identity MyDoc4
Add-PnPFolder -Name $folderName -Folder $library.RootFolder.Name
$folderURL=[string]::Format("{0}/{1}", $library.RootFolder.ServerRelativeUrl,$folderName)
$folder=Get-PnPFolder -Url $folderURL
Set-PnPListItem -List "MyDoc4" -Identity $folder.ListItemAllFields -Values #{Editor=""+$user.Id+"";Modified="7/24/2019"}

Remotely access \\server\c$\users\user\My Documents

I'm trying to remotely get the size of a users 'My Documents' folder using the C$ built in share.
I can browse the share, I can 'Set-Location' to the share but as soon as I try to 'Get-ChildItem' I get a permission denied.
I can't figure out if this is some built in limitation of Powershell?
Currently tried on PS2, PS3 same result.
(User has full access on both share and NTFS)
I've tried with providing the credentials using 'Get-Credentials' and I have also tried with 'New-PSDrive' mappings as well, same issue, the location is fine but as soon as I GCI it spits out 'PermissionDenied'.
$compList = [LIST OF COMPUTERS]
$exclude = [LIST OF EXCLUDED USERS]
$userSizes = #()
foreach ($computer in $compList){
gci ("\\$computer\c$\users\") | where {$exclude -notcontains $_.name}| foreach-object {
$curUser = $_.name
New-PSDrive -name "Map" -PSProvider FileSystem -Root "\\$computer\c$\users\$_\My Documents"
$size = "{0:N2}" -f ((gci "Map:\" -recurse | Measure-Object -property length -sum).sum /1MB)
$properties = #{'Computer'=$computer;'User'=$curUser;'Size (MB)'=$size}
$curObject = New-Object –TypeName PSObject –Prop $properties
$userSizes += $curObject
Remove-PSDrive -name "Map"
}
}
$userSizes | Out-GridView
$usersizes = $null
Keep in mind that GCI in PS2 doesn't allow providing credentials and the 'FileSystem' provider doesn't either!
You might need credentials to use Get-ChildItem on a remote share, i've had it happen that i've had full access to my NAS but powershell gave me the same error "Permission Denied", it seems weird and i can't why it failed when i had full permissions but it worked when i gave powershell my credentials.
Try declaring credentials first:
$creds = get-credential
then using the credentials like so
Get-ChildItem "\\server\c$\users\user\My Documents" -credentials $creds
Uggh what a disgrace.
The reason was that the path is actually
\\[server]\c$\users\[user]\documents
For some unknown, god forsaken reason, Windows Explorer displays the path as 'My Documents' but the actual path is 'Documents'.
I have no idea why they would do this but there it is. Working fine now, another few hours wasted...