Created
March 23, 2013 20:54
-
-
Save mmdemirbas/5229315 to your computer and use it in GitHub Desktop.
PowerShell script to set or clear NTFS read-only flag of a volume by volume label
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
######################################################################### | |
# # | |
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@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.