How to avoid cloud drive scanning your folders -- use PowerShell and 7z to individually encrypt the files inside an entire folder
In order to prevent the cloud drive from scanning my files, I need to encrypt all the content I upload. I use 7z software to encrypt individually. My requirements: Encrypt all files and folders in the entire directory
In order to prevent the cloud drive from scanning my files, I need to encrypt all the content I upload. I use 7z software to encrypt individually. My requirements:
- Encrypt all files and folders in the entire directory individually and export them to another directory
- Preserve the original directory structure for all files and folders
- Store without compression (so that 7z can encrypt quickly)
This is a test folder
D:.
โ beach-dominican-republic-dominican-caribbean-thumb.jpg
โ ็ ๆตใฎไธ.jpg
โ
โโโโ3ไบบz#@@%R@$%T434b4y
โ autumn-forest-woods-nature-thumb.jpg
โ D'accord.jpg
โ ะะฐะฐะปะณะฐะดะฐ.jpg
โ ะฅะพัะพัะพ.jpg
โ ๆขฆๅนปๆ็ป็ปฟ่ฒ้ฃๆฏๆก้ขๅฃ็บธ.jpg
โ
โโโโdfใใใฃใght3%^&&$&%%
โ โ Hoy.jpg
โ โ No, ยฟverdad.jpg
โ โ ยฟQuiรฉn eres.jpg
โ โ
โ โโโโEstรก bien, no se puede
โ cars-old-vintage-automobiles-classic-preview.jpg
โ casserole-seafood-paella-food-preview.jpg
โ ๆฐใใๆฅใฆใใ ใใ.jpg
โ ๆๆฅใซใใพใใใ.jpg
โ
โโโโุญุณููุงreq!!f fgr
โ Neu hier.jpg
โ ุฑุจู
ุง ุบุฏูุง.jpg
โ ูุง ุจุงูุชุฃููุฏ.jpg
โ ูุง ูู
ูู.jpg
โ
โโโโไบๅฅ๏ผ๏ผ#๏ฟฅ#
burger-cheese-vegetarian-halloumi-preview.jpg
cake-cafeteria-desserts-dessert-preview.jpg
I had AI write a PowerShell script test.ps1๏ผ
# Configuration Parameters
$sourceDir = "D:\test\private thing" # Source directory to be encrypted
$targetDir = "D:\test\private thing - Encryption" # Target root directory for storing encrypted files
$password = "123" # password
$sevenZipPath = "C:\Program Files\7-Zip\7z.exe" # 7z path
# Check if 7z exists
if (-not (Test-Path $sevenZipPath)) {
Write-Error "Cannot find 7z.exe, please check the path"
exit 1
}
# Get all files (recursive)
Get-ChildItem -Path $sourceDir -File -Recurse | ForEach-Object {
$file = $_
# Calculate relative path
$relativePath = $file.Directory.FullName.Substring($sourceDir.Length).TrimStart('\')
# Construct target directory path
if ($relativePath) {
$targetFilePath = Join-Path $targetDir $relativePath
} else {
$targetFilePath = $targetDir
}
# Create the target directory (if it does not exist)
if (-not (Test-Path $targetFilePath)) {
New-Item -ItemType Directory -Path $targetFilePath -Force | Out-Null
}
# Construct 7z file path
$archiveName = $file.Name + ".7z"
$archivePath = Join-Path $targetFilePath $archiveName
# Compress and encrypt using 7z
& "$sevenZipPath" a -mx0 "-p$password" -mhe=on "$archivePath" "$($file.FullName)" -y
if ($LASTEXITCODE -eq 0) {
Write-Host "Encrypted: $($file.FullName) -> $archivePath" -ForegroundColor Green
} else {
Write-Host "Encryption failed: $($file.FullName)" -ForegroundColor Red
}
}
Write-Host "`nAll file processing is complete!" -ForegroundColor Green
Among them, $sourceDir, $targetDir, $password, and $sevenZipPath are variables that need to be modified according to your own needs.
Run a script using PowerShell
powershell -ExecutionPolicy Bypass -File .\test.ps1
After the script execution is complete, this is the output folder structure
D:.
โ beach-dominican-republic-dominican-caribbean-thumb.jpg.7z
โ ็ ๆตใฎไธ.jpg.7z
โ
โโ3ไบบz#@@%R@$%T434b4y
โ autumn-forest-woods-nature-thumb.jpg.7z
โ D'accord.jpg.7z
โ ะะฐะฐะปะณะฐะดะฐ.jpg.7z
โ ะฅะพัะพัะพ.jpg.7z
โ ๆขฆๅนปๆ็ป็ปฟ่ฒ้ฃๆฏๆก้ขๅฃ็บธ.jpg.7z
โ
โโdfใใใฃใght3%^&&$&%%
โ โ Hoy.jpg.7z
โ โ No, ยฟverdad.jpg.7z
โ โ ยฟQuiรฉn eres.jpg.7z
โ โ
โ โโEstรก bien, no se puede
โ cars-old-vintage-automobiles-classic-preview.jpg.7z
โ casserole-seafood-paella-food-preview.jpg.7z
โ ๆฐใใๆฅใฆใใ ใใ.jpg.7z
โ ๆๆฅใซใใพใใใ.jpg.7z
โ
โโุญุณููุงreq!!f fgr
โ Neu hier.jpg.7z
โ ุฑุจู
ุง ุบุฏูุง.jpg.7z
โ ูุง ุจุงูุชุฃููุฏ.jpg.7z
โ ูุง ูู
ูู.jpg.7z
โ
โโไบๅฅ๏ผ๏ผ#๏ฟฅ#
burger-cheese-vegetarian-halloumi-preview.jpg.7z
cake-cafeteria-desserts-dessert-preview.jpg.7z
This script can complete the task.
If you don't want the names to be known either, you can directly convert all paths into MD5 encoding.
# Configuration Parameters
$sourceDir = "D:\test\private thing" # Source directory to be encrypted
$targetDir = "D:\test\private thing - Encryption" # Target root directory for storing encrypted files
$password = "123" # password
$sevenZipPath = "C:\Program Files\7-Zip\7z.exe" # 7z path
# Create MD5 hash function
function Get-MD5Hash {
param([string]$InputString)
$md5 = [System.Security.Cryptography.MD5]::Create()
$bytes = [System.Text.Encoding]::UTF8.GetBytes($InputString)
$hash = $md5.ComputeHash($bytes)
# Convert to lowercase hexadecimal string
return ($hash | ForEach-Object { $_.ToString("x2") }) -join ''
}
# Check if 7z exists
if (-not (Test-Path $sevenZipPath)) {
Write-Error "Cannot find 7z.exe, please check the path"
exit 1
}
# Get all files (recursive)
Get-ChildItem -Path $sourceDir -File -Recurse | ForEach-Object {
$file = $_
# Calculate relative path
$relativePath = $file.Directory.FullName.Substring($sourceDir.Length).TrimStart('\')
# Split the path and hash each folder separately
if ($relativePath) {
$pathParts = $relativePath.Split('\')
$hashedPathParts = @()
foreach ($part in $pathParts) {
# Perform MD5 hashing on each folder name
$hashedPart = Get-MD5Hash -InputString $part
$hashedPathParts += $hashedPart
}
# Recombining the hashed path
$hashedPath = $hashedPathParts -join '\'
$targetFilePath = Join-Path $targetDir $hashedPath
} else {
$targetFilePath = $targetDir
}
# Create the target directory (if it does not exist)
if (-not (Test-Path $targetFilePath)) {
New-Item -ItemType Directory -Path $targetFilePath -Force | Out-Null
}
# Perform MD5 hashing on the entire file name (including the extension)
$fullFileName = $file.Name
$hashedFileName = Get-MD5Hash -InputString $fullFileName
# Construct 7z file path
$archiveName = $hashedFileName + ".7z"
$archivePath = Join-Path $targetFilePath $archiveName
# Compress and encrypt using 7z
& "$sevenZipPath" a -mx0 "-p$password" -mhe=on "$archivePath" "$($file.FullName)" -y
if ($LASTEXITCODE -eq 0) {
Write-Host "Encrypted: $($file.FullName) -> $archivePath" -ForegroundColor Green
} else {
Write-Host "Encryption failed: $($file.FullName)" -ForegroundColor Red
}
}
Write-Host "`nAll file processing is complete!" -ForegroundColor Green
After this, the encrypted folder:
D:.
โ 43794276ebe0b4a5c9ac57c9f3d22898.7z
โ 6c5aa696b01f76db7ef074536b03ed9a.7z
โ
โโ0eb68956079d3cb6962dc2dc49f01be4
โ โ 2763897780a0e2e645ec4d2e41556306.7z
โ โ 430890ddd152b3c455a1478279e47a7b.7z
โ โ c430b7629217efeea6badf39a65cc058.7z
โ โ
โ โโ21e933b76e03210646eede333e76b4dd
โ 42bde53eaf003f3dc4f3738294ae1464.7z
โ 612faac79f62c16eaeacc2f75fbe19c8.7z
โ 841d69a443b3701e7e9f55c511c58892.7z
โ e80cd4e138ecfae6846fd301b3f1a902.7z
โ
โโ2864dfea338f51a4579757ef81773061
โ 456532e2f62fc75477bd0f1e1476a13b.7z
โ 5ea01eb5a08b21d42e2f209a3fa713bf.7z
โ 735dbc38b8fb2b016c660cdfb2b242df.7z
โ c47fcce46e5e29e8f039acbeecf85647.7z
โ cb3af4320d1388fa6cfbe98a6dae91f5.7z
โ
โโfe9e0e69fda49d513796cd5fdca66f24
โ 1b719344629ccf638066ecd2781cdd5d.7z
โ 635164eead60d30732f45a464811333a.7z
โ b9d1f953b32f034acda5b049ee6797f8.7z
โ e8f3dc25e3e56ce0d8bdc560dd267f76.7z
โ
โโ42fb902ad1ed5cee820e4b314324e29a
4f82a3dde34e3eb1c9e42e610882001e.7z
f19a29f5e526cf04369ea2a3ddd67c55.7z
But in this way, I won't be able to know which encrypted file path corresponds to the original file. I need to output an encrypted txt file to store these mappings. So the script later became like this.
# Configuration Parameters
$sourceDir = "D:\test\private thing" # Source directory to be encrypted
$targetDir = "D:\test\private thing - Encryption" # Target root directory for storing encrypted files
$password = "123" # password
$sevenZipPath = "C:\Program Files\7-Zip\7z.exe" # 7z path
$mapFileName = "encryption_map.txt" # Mapping file name
$mapFile = Join-Path $targetDir $mapFileName # Full path to mapping file
# Create MD5 hash function
function Get-MD5Hash {
param([string]$InputString)
$md5 = [System.Security.Cryptography.MD5]::Create()
$bytes = [System.Text.Encoding]::UTF8.GetBytes($InputString)
$hash = $md5.ComputeHash($bytes)
# Convert to lowercase hexadecimal string
return ($hash | ForEach-Object { $_.ToString("x2") }) -join ''
}
# Initialize mapping content
$mappingContent = @"
ENCRYPTION MAPPING FILE
===============================================
Generated on: $(Get-Date)
Original Path -> Encrypted Path
===============================================
"@
# Check if 7z exists
if (-not (Test-Path $sevenZipPath)) {
Write-Error "Cannot find 7z.exe, please check the path"
exit 1
}
# Create target directory if it doesn't exist
if (-not (Test-Path $targetDir)) {
New-Item -ItemType Directory -Path $targetDir -Force | Out-Null
}
# Get all files (recursive)
Get-ChildItem -Path $sourceDir -File -Recurse | ForEach-Object {
$file = $_
# Calculate relative path
$relativePath = $file.Directory.FullName.Substring($sourceDir.Length).TrimStart('\')
# Split the path and hash each folder separately
if ($relativePath) {
$pathParts = $relativePath.Split('\')
$hashedPathParts = @()
$originalPathParts = @()
foreach ($part in $pathParts) {
# Store original folder name for mapping
$originalPathParts += $part
# Perform MD5 hashing on each folder name
$hashedPart = Get-MD5Hash -InputString $part
$hashedPathParts += $hashedPart
}
# Recombining the hashed path
$hashedPath = $hashedPathParts -join '\'
$targetFilePath = Join-Path $targetDir $hashedPath
# Store original folder path for mapping
$originalFolderPath = $originalPathParts -join '\'
} else {
$targetFilePath = $targetDir
$originalFolderPath = ""
}
# Create the target directory (if it does not exist)
if (-not (Test-Path $targetFilePath)) {
New-Item -ItemType Directory -Path $targetFilePath -Force | Out-Null
}
# Perform MD5 hashing on the entire file name (including the extension)
$fullFileName = $file.Name
$hashedFileName = Get-MD5Hash -InputString $fullFileName
# Construct 7z file path
$archiveName = $hashedFileName + ".7z"
$archivePath = Join-Path $targetFilePath $archiveName
# Compress and encrypt using 7z
& "$sevenZipPath" a -mx0 "-p$password" -mhe=on "$archivePath" "$($file.FullName)" -y
if ($LASTEXITCODE -eq 0) {
Write-Host "Encrypted: $($file.FullName) -> $archivePath" -ForegroundColor Green
# Prepare mapping entry
if ($originalFolderPath) {
$originalRelativePath = "\$originalFolderPath\$fullFileName"
$encryptedRelativePath = "\$hashedPath\$archiveName"
} else {
$originalRelativePath = "\$fullFileName"
$encryptedRelativePath = "\$archiveName"
}
# Add to mapping content
$mappingContent += "ORIGINAL: $originalRelativePath`r`n"
$mappingContent += "ENCRYPTED: $encryptedRelativePath`r`n"
$mappingContent += "-" * 60 + "`r`n"
Write-Host " Mapping: $originalRelativePath --> $encryptedRelativePath" -ForegroundColor Yellow
} else {
Write-Host "Encryption failed: $($file.FullName)" -ForegroundColor Red
}
}
# Add footer to mapping content
$mappingContent += "`r`n"
$mappingContent += "===============================================`r`n"
$mappingContent += "Total files processed: $($mappingContent.Split('ORIGINAL:').Count - 1)`r`n"
$mappingContent += "End of mapping file`r`n"
$mappingContent += "===============================================`r`n"
# Save the mapping file temporarily as unencrypted
$tempMapFile = $mapFile
Set-Content -Path $tempMapFile -Value $mappingContent -Encoding UTF8
Write-Host "`nCreating encrypted mapping file..." -ForegroundColor Cyan
# Encrypt the mapping file with the same password
$encryptedMapFile = $mapFile + ".7z"
& "$sevenZipPath" a -mx0 "-p$password" -mhe=on "$encryptedMapFile" "$tempMapFile" -y
if ($LASTEXITCODE -eq 0) {
# Remove the temporary unencrypted file
Remove-Item $tempMapFile -Force
Write-Host "Mapping file created and encrypted: $encryptedMapFile" -ForegroundColor Green
Write-Host "" -ForegroundColor Green
Write-Host "IMPORTANT NOTES:" -ForegroundColor Yellow
Write-Host "1. The mapping file is encrypted with the same password: $password" -ForegroundColor Yellow
Write-Host "2. To view the mapping file, use: 7z e $encryptedMapFile -p$password" -ForegroundColor Yellow
Write-Host "3. Keep this password safe - without it you cannot recover the original file names!" -ForegroundColor Red
} else {
Write-Host "Failed to encrypt mapping file!" -ForegroundColor Red
Write-Host "Unencrypted mapping file is saved as: $tempMapFile" -ForegroundColor Yellow
}
Write-Host "`nAll file processing is complete!" -ForegroundColor Green
It will eventually output an encryption_map.txt.7z file in the root path of the target directory, using the same encryption password. After decryption, you can see the mapping of file paths before and after encryption. This file can be used to find the original file locations:
ENCRYPTION MAPPING FILE
===============================================
Generated on: 03/02/2026 20:36:47
Original Path -> Encrypted Path
===============================================
ORIGINAL: \beach-dominican-republic-dominican-caribbean-thumb.jpg
ENCRYPTED: \6c5aa696b01f76db7ef074536b03ed9a.7z
------------------------------------------------------------
ORIGINAL: \็ ๆตใฎไธ.jpg
ENCRYPTED: \43794276ebe0b4a5c9ac57c9f3d22898.7z
------------------------------------------------------------
ORIGINAL: \3ไบบz#@@%R@$%T434b4y\autumn-forest-woods-nature-thumb.jpg
ENCRYPTED: \2864dfea338f51a4579757ef81773061\c47fcce46e5e29e8f039acbeecf85647.7z
------------------------------------------------------------
ORIGINAL: \3ไบบz#@@%R@$%T434b4y\D'accord.jpg
ENCRYPTED: \2864dfea338f51a4579757ef81773061\456532e2f62fc75477bd0f1e1476a13b.7z
------------------------------------------------------------
ORIGINAL: \3ไบบz#@@%R@$%T434b4y\ะะฐะฐะปะณะฐะดะฐ.jpg
ENCRYPTED: \2864dfea338f51a4579757ef81773061\cb3af4320d1388fa6cfbe98a6dae91f5.7z
------------------------------------------------------------
ORIGINAL: \3ไบบz#@@%R@$%T434b4y\ะฅะพัะพัะพ.jpg
ENCRYPTED: \2864dfea338f51a4579757ef81773061\5ea01eb5a08b21d42e2f209a3fa713bf.7z
------------------------------------------------------------
ORIGINAL: \3ไบบz#@@%R@$%T434b4y\ๆขฆๅนปๆ็ป็ปฟ่ฒ้ฃๆฏๆก้ขๅฃ็บธ.jpg
ENCRYPTED: \2864dfea338f51a4579757ef81773061\735dbc38b8fb2b016c660cdfb2b242df.7z
------------------------------------------------------------
ORIGINAL: \dfใใใฃใght3%^&&$&%%\Hoy.jpg
ENCRYPTED: \0eb68956079d3cb6962dc2dc49f01be4\2763897780a0e2e645ec4d2e41556306.7z
------------------------------------------------------------
ORIGINAL: \dfใใใฃใght3%^&&$&%%\No, ยฟverdad.jpg
ENCRYPTED: \0eb68956079d3cb6962dc2dc49f01be4\c430b7629217efeea6badf39a65cc058.7z
------------------------------------------------------------
ORIGINAL: \dfใใใฃใght3%^&&$&%%\ยฟQuiรฉn eres.jpg
ENCRYPTED: \0eb68956079d3cb6962dc2dc49f01be4\430890ddd152b3c455a1478279e47a7b.7z
------------------------------------------------------------
ORIGINAL: \dfใใใฃใght3%^&&$&%%\Estรก bien, no se puede\cars-old-vintage-automobiles-classic-preview.jpg
ENCRYPTED: \0eb68956079d3cb6962dc2dc49f01be4\21e933b76e03210646eede333e76b4dd\841d69a443b3701e7e9f55c511c58892.7z
------------------------------------------------------------
ORIGINAL: \dfใใใฃใght3%^&&$&%%\Estรก bien, no se puede\casserole-seafood-paella-food-preview.jpg
ENCRYPTED: \0eb68956079d3cb6962dc2dc49f01be4\21e933b76e03210646eede333e76b4dd\e80cd4e138ecfae6846fd301b3f1a902.7z
------------------------------------------------------------
ORIGINAL: \dfใใใฃใght3%^&&$&%%\Estรก bien, no se puede\ๆฐใใๆฅใฆใใ ใใ.jpg
ENCRYPTED: \0eb68956079d3cb6962dc2dc49f01be4\21e933b76e03210646eede333e76b4dd\612faac79f62c16eaeacc2f75fbe19c8.7z
------------------------------------------------------------
ORIGINAL: \dfใใใฃใght3%^&&$&%%\Estรก bien, no se puede\ๆๆฅใซใใพใใใ.jpg
ENCRYPTED: \0eb68956079d3cb6962dc2dc49f01be4\21e933b76e03210646eede333e76b4dd\42bde53eaf003f3dc4f3738294ae1464.7z
------------------------------------------------------------
ORIGINAL: \ุญุณููุงreq!!f fgr\Neu hier.jpg
ENCRYPTED: \fe9e0e69fda49d513796cd5fdca66f24\e8f3dc25e3e56ce0d8bdc560dd267f76.7z
------------------------------------------------------------
ORIGINAL: \ุญุณููุงreq!!f fgr\ุฑุจู
ุง ุบุฏูุง.jpg
ENCRYPTED: \fe9e0e69fda49d513796cd5fdca66f24\1b719344629ccf638066ecd2781cdd5d.7z
------------------------------------------------------------
ORIGINAL: \ุญุณููุงreq!!f fgr\ูุง ุจุงูุชุฃููุฏ.jpg
ENCRYPTED: \fe9e0e69fda49d513796cd5fdca66f24\635164eead60d30732f45a464811333a.7z
------------------------------------------------------------
ORIGINAL: \ุญุณููุงreq!!f fgr\ูุง ูู
ูู.jpg
ENCRYPTED: \fe9e0e69fda49d513796cd5fdca66f24\b9d1f953b32f034acda5b049ee6797f8.7z
------------------------------------------------------------
ORIGINAL: \ุญุณููุงreq!!f fgr\ไบๅฅ๏ผ๏ผ#๏ฟฅ#\burger-cheese-vegetarian-halloumi-preview.jpg
ENCRYPTED: \fe9e0e69fda49d513796cd5fdca66f24\42fb902ad1ed5cee820e4b314324e29a\f19a29f5e526cf04369ea2a3ddd67c55.7z
------------------------------------------------------------
ORIGINAL: \ุญุณููุงreq!!f fgr\ไบๅฅ๏ผ๏ผ#๏ฟฅ#\cake-cafeteria-desserts-dessert-preview.jpg
ENCRYPTED: \fe9e0e69fda49d513796cd5fdca66f24\42fb902ad1ed5cee820e4b314324e29a\4f82a3dde34e3eb1c9e42e610882001e.7z
------------------------------------------------------------
===============================================
Total files processed: 263
End of mapping file
===============================================
At this stage, you can avoid your files being scanned by certain cloud drive services. They cannot know the file names, cannot unzip compressed files, and cannot view the list of files inside the compressed files. All your data is incomprehensible to them. For yourself, the most important thing is to properly save the encryption_map.txt.7z file and the password.
If you need to specify the input and output directories in the parameters, rather than hardcoding them in the script, you can modify it like this directly.
Delete
$sourceDir = "D:\test\private thing"
$targetDir = "D:\test\private thing - Encryption"
Replace with
param(
[Parameter(Mandatory=$true)]
[string]$sourceDir,
[Parameter(Mandatory=$true)]
[string]$targetDir
)
This way, when executing the script, it can be
.\test.ps1 -sourceDir "D:\test\private thing" -targetDir "D:\test\private thing - Encryption"
Or use positional arguments
.\test.ps1 "D:\test\private thing" "D:\test\private thing - Encryption"
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes โ full credit and traffic to the original publisher.