Skip to content

Instantly share code, notes, and snippets.

@MyITGuy
Created February 10, 2025 20:54
Show Gist options
  • Save MyITGuy/4fc9c13c99785e6895d4dd3eebcc83d1 to your computer and use it in GitHub Desktop.
Save MyITGuy/4fc9c13c99785e6895d4dd3eebcc83d1 to your computer and use it in GitHub Desktop.
function Get-Owner {
[CmdletBinding()]
PARAM(
[Parameter(Mandatory = $false, Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[ValidateScript({
$Item = $_ | Get-Item
(Test-Path -Path $Item) -eq $true -and $Item.GetType().FullName -in @('System.IO.FileInfo', 'System.IO.DirectoryInfo')
})]
$Path
)
$Acl = Get-Acl -Path $Path
[System.Security.Principal.NTAccount]($Acl.Owner)
}
function Set-Owner {
[CmdletBinding()]
PARAM(
[Parameter(Mandatory = $false, Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[ValidateScript({
$Item = $_ | Get-Item
(Test-Path -Path $Item) -eq $true -and $Item.GetType().FullName -in @('System.IO.FileInfo', 'System.IO.DirectoryInfo')
})]
$Path
,
[System.Security.Principal.NTAccount]
$NewOwner
)
$NTAccount_Owner_New = [System.Security.Principal.NTAccount]$NewOwner
Write-Verbose "NTAccount_Owner_New: $($NTAccount_Owner_New)"
$Acl = Get-Acl -Path $Path
$NTAccount_Owner_Current = [System.Security.Principal.NTAccount]($Acl.Owner)
Write-Verbose "NTAccount_Owner_Current: $($NTAccount_Owner_Current)"
$AlreadySet = $NTAccount_Owner_Current -eq $NTAccount_Owner_New
Write-Verbose "AlreadySet: $($AlreadySet)"
if (-not $AlreadySet) {
$Acl.SetOwner($NTAccount_Owner_New)
Set-Acl -Path $Item.FullName -AclObject $Acl
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment