Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursively set file attributes

Tags:

powershell

This code:

Get-ChildItem $targetConfig -Recurse | Set-ItemProperty -Name IsReadOnly -Value $false

Returns several errors:

Set-ItemProperty : Property System.Boolean IsReadOnly=False does not exist. At line:1 char:56 + Get-ChildItem $targetConfig -Recurse | Set-ItemProperty <<<< -Name IsReadOnly -Value $false + CategoryInfo : ReadError: (System.Boolean IsReadOnly=False:PSNoteProperty) [Set-ItemProperty], IOException + FullyQualifiedErrorId : SetPropertyError,Microsoft.PowerShell.Commands.SetItemPropertyCommand

What this errors means?

like image 337
Vitaliy Avatar asked Jan 16 '13 21:01

Vitaliy


Video Answer


1 Answers

It happens because:

Get-ChildItem $targetConfig -Recurse

Return both DirectoryInfo and FileInfo. And Set-ItemProperty fails on setting "ReadOnly" for DirectoryInfo.

To handle this use:

Get-ChildItem $targetConfig -Recurse |
    Where-Object {$_.GetType().ToString() -eq "System.IO.FileInfo"} |
    Set-ItemProperty -Name IsReadOnly -Value $false
like image 169
Vitaliy Avatar answered Oct 17 '22 18:10

Vitaliy