-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathRemoveLocalOneDriveFiles.ps1
More file actions
60 lines (47 loc) · 1.86 KB
/
RemoveLocalOneDriveFiles.ps1
File metadata and controls
60 lines (47 loc) · 1.86 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<#
.SYNOPSIS
Removes local copies of OneDrive files while keeping them online.
.DESCRIPTION
This script goes through your OneDrive folder and removes the local copies of files by marking them as "Online-Only" using the `attrib` command. This frees up disk space without deleting the files from the cloud.
.NOTES
Author: Emanuele Bartolesi
Version: 1.0
Created: 2024-11-17
.PARAMETER OneDrivePath
Path to the local OneDrive folder (default: "$env:USERPROFILE\OneDrive").
.EXAMPLE
./RemoveLocalOneDriveFiles.ps1
.EXAMPLE
./RemoveLocalOneDriveFiles.ps1 -OneDrivePath "C:\Users\YourName\OneDrive"
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $false)]
[string]$OneDrivePath = "$env:USERPROFILE\OneDrive"
)
# Function to free up space for a specific folder
function Free-UpSpace {
param (
[string]$Path
)
Write-Host "Processing folder: $Path" -ForegroundColor Yellow
if (Test-Path $Path) {
# Mark all files in the folder as "Online-Only" using the `attrib` command
Get-ChildItem -Path $Path -Recurse -File | ForEach-Object {
try {
Write-Verbose "Setting file as 'Online-Only': $($_.FullName)"
attrib +U -P $_.FullName
} catch {
Write-Warning "Failed to process file: $($_.FullName). Error: $_"
}
}
Write-Host "Local files in '$Path' are now 'Online-Only'." -ForegroundColor Green
} else {
Write-Warning "The path '$Path' does not exist. Please check the OneDrive path."
}
}
# Main script execution
Write-Host "Removing local copies of OneDrive files..." -ForegroundColor Cyan
# Call the function to free up space
Free-UpSpace -Path $OneDrivePath
Write-Host "Local OneDrive files have been set to 'Online-Only'." -ForegroundColor Green