ファイルに属性を設定する - PowerShell

PowerShellでファイルに属性を設定するコマンドを紹介します。

概要

PowerShellでファイルに属性を設定する場合は、Set-ItemProperty コマンドを利用します。

書式

Set-ItemProperty -path (属性を変更するファイル名) -name attributes -value (属性値)
属性値には以下の値を設定できます。複数の値を設定する場合は","で区切り設定します。

属性値意味
Archiveアーカイブ属性
Readonly読み取り専用
Hidden隠しファイル
Systemシステムファイル
Normal属性を設定しない

ReadOnly属性の場合は下記のコマンドも利用できます。
Set-ItemProperty -path (属性を変更するファイル名) -name IsReadOnly -value ($true または $false)

コマンド例

Set-ItemProperty -path test.png -name attributes -value "Readonly"
Set-ItemProperty -path app.ini -name attributes -value "Hidden,System"
Set-ItemProperty -path memo.txt -name attributes -value "Normal"
Set-ItemProperty -path memo.txt -name IsReadOnly -value $true

PowerShellでGet-ChildItemコマンドでディレクトリのファイル一覧を表示します。doc.txtには属性が設定されていないことがわかります。


下記のコマンドを実行して、Archive属性とReadOnly属性を付与します。
Set-ItemProperty -path .\doc.txt -name attributes -value "Archive,Readonly"


Get-ChildItemコマンドでファイル一覧を表示します。doc.txt の属性表示に"a" "r" の文字が表示され、Archive属性とReadOnly属性が追加されたことが確認できます。


続いてディレクトリのReadOnly属性を解除します。
下記のコマンドを実行します。
Set-ItemProperty -path .\DataFolder -name attributes -value "Normal"



Get-ChildItemコマンドでファイル一覧を表示します。DataFolderディレクトリのReadOnly属性が解除されたことが確認できます。


著者
iPentecのメインプログラマー
C#, ASP.NET の開発がメイン、少し前まではDelphiを愛用
最終更新日: 2024-01-06
作成日: 2018-11-11
iPentec all rights reserverd.