-
-
Save mmdemirbas/5229315 to your computer and use it in GitHub Desktop.
######################################################################### | |
# # | |
# Script to set or clear read-only flag of an NTFS volume. # | |
# # | |
# Usage: .\set-ntfs-ro.ps1 set "MY DISK LABEL" # | |
# .\set-ntfs-ro.ps1 clear "MY DISK LABEL" # | |
# # | |
# Author: Muhammed Demirbas, mmdemirbas at gmail dot com # | |
# Date : 2013-03-23 # | |
# # | |
######################################################################### | |
param($setOrClear, $diskLabel) | |
if( [string]::IsNullOrWhiteSpace($setOrClear) ) | |
{ | |
$ScriptName = $MyInvocation.MyCommand.Name | |
"usage: .\$ScriptName set ""MY DISK LABEL""" | |
" .\$ScriptName clear ""MY DISK LABEL""" | |
return | |
} | |
if( $setOrClear -ne "set" -and $setOrClear -ne "clear" ) | |
{ | |
throw 'Valid actions are "set" and "clear"!' | |
} | |
if( [string]::IsNullOrWhiteSpace($diskLabel) ) | |
{ | |
throw "Please specify a non-blank disk label!" | |
} | |
# Path of the temporary file to use as diskpart script | |
$scriptFile = "$env:TMP\set-ntfs-ro-script.tmp" | |
# Save "list volume" command to a temp-file | |
"list volume" | Out-File -Encoding ascii $scriptFile | |
# Execute diskpart providing the script, and select the involved line | |
$matches = diskpart /s $scriptFile | Select-String $diskLabel | |
if( $matches.Length -eq 0 ) | |
{ | |
throw "No match for the label: $diskLabel" | |
} | |
elseif ( $matches.Length -ge 2 ) | |
{ | |
throw "More than one match for the label: $diskLabel" | |
} | |
# Obtain volume number | |
$words = $matches.Line.Trim().Split(" ") | |
if( !$words -or $words.Length -le 1 ) | |
{ | |
throw "Volume number couldn't be obtained for the volume:`n$line" | |
} | |
$volumeNum = $words.Get(1) | |
# Save the command to modify read-only flag to a temp-file | |
"select volume $volumeNum | |
att vol $setOrClear readonly | |
detail vol" | Out-File -Encoding ascii $scriptFile | |
# Execute the command, and print details | |
diskpart /s $scriptFile | |
# Clean the waste | |
del $scriptFile |
Hi !
@lxandr : Can you please explain the usage and function of the first skript (mk-USB-GPT-NTFS.ps1).
Unfortunately I dont understand it and it seems a little bit risky for me to try without understanding . THANKS !!
BR BB10
@backbone10 sure! (strange, but your comment appeared here only now - months later, and so I didn't see it before, sorry).
First function "Check-Admin" checks if the script is run with elevated privileges. And if it's not, it tries to re-run itself with elevated privileges through UAC (elevation is needed to work with disk devices). If we remove this part from script, we must run this script with elevated privileges every time by hand. So this part of code just simplifies things - it's simpler to run script from user without administrative privileges - UAC window automatically appears and asks for user and password to elevate privileges.
Here we get a list of drives connected via usb (filtering all disks except usb) and print them:
Get-Disk | Where-Object IsSystem -eq $False | Where-Object -FilterScript {$_.BusType -Eq "USB"}
Then, we ask user to enter disk number from this list (in case more than one usb drive is connected):
$USBDrive = Read-Host -Prompt 'Enter USB disk number (or press "Enter" for exit)'
Then we make extra safety checks, and ask user if he sure to continue with deleting all partitions from that usb disk (Clear-Disk) and Creating GPT-style NTFS partition on it:
Set-Disk -PartitionStyle GPT, New-Partition ..., Format-Volume -FileSystem NTFS
I don’t know how this script will behave on windows 7 system - because of older default windows 7's PowerShell version.
I tested these scripts only on windows 8.1 64 bit. (so, on windows 10 they should work too).
It's better to test things under VirtualBox.
Hi! Nice script! I've digged into it and surprisingly came to even simpler solution:
as you can see there is no need in "diskpart" now! Cool!
To enable write, just change two last lines:
I've tested this script on my usb harddrive (fixed disk) with GPT(!) NTFS partition and it works. But, it DOESN'T work on usb removable drives as flash drives - you'll get an error from "Set-Partition"!
Also, about read-only usb flash drives. My test shows, that it is plausible... But:
To automate this, I wrote 3 scripts (they work only with usb removable drives! also, they auto-elevate themselves through UAC):
mk-USB-GPT-NTFS.ps1
USB_GPT_set_read-only.ps1
USB_GPT_enable_write.ps1
Also forgot to mention: