LoginSignup
8
10

More than 5 years have passed since last update.

PowerShellでフォルダのアクセス権限を設定する

Posted at

やりたいこと

mkdirコマンドでフォルダを作ったりした時にアクセス権限を設定したい。
GUIでポチポチするのが面倒だからスクリプトにしてしまいたい。

やりかた

  1. アクセス権限を設定するための配列をつくる
  2. 1.で作った配列を基にFileSystemAccessRuleのオブジェクトをつくる
  3. 対象フォルダのACLを取得し、2.で作ったFileSystemAccessRuleをセットする
  4. 対象フォルダに3.のACLをセットする
hoge.ps1
mkdir fugafuga
# アクセス権限設定のための配列
# 1つ目の要素がアクセス権限を設定するユーザー/グループ名
$permission = ("IUSR", [System.Security.AccessControl.FileSystemRights]::ReadAndExecute, [System.Security.AccessControl.InheritanceFlags]::ContanerInherit, [System.Security.AccessControl.InheritanceFlags]::ObjectInherit), [System.Security.AccessControl.PropagationFlags]::None, [System.Security.AccessControl.AccessControlType]::Allow)
# FileSystemAccessRuleオブジェクトの作成
$accessRule = New-Object -TypeNames System.Security.AccessControl.FileSystemAccessRule $permission
# フォルダにアクセス権限をセットする
$targetPath = "fugafuga"
$targetACL = Get-Acl $targetPath
$targetACL.SetAccessRule($accessRule)
$targetACL | Set-Acl $targetPath

よく使いそうな[System.Security.AccessControl.FileSystemRights]

  • Read:読み取り
  • ReadAndExecute:読み取りと実行
  • Modify:変更
  • FullControl:フルコントロール
8
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
10