public
Created

PowerShell script to set or clear NTFS read-only flag of a volume by volume label

  • Download Gist
set-ntfs-ro.ps1
PowerShell
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 61 62 63 64 65 66 67
#########################################################################
# #
# 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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.