when using private keys (.pem) files in Windows, you may encounter a permission error stating that the file is "too open". follow the procedure below to resolve this.
# Define the file path
$filePath = "C:\{path\to\your\file}"
# Define the target user (format: DOMAIN\username or just username for local accounts)
$targetUser = "{username}" # Replace with the desired username
# Change the owner to the specified user
$acl = Get-Acl -Path $filePath
$acl.SetOwner([System.Security.Principal.NTAccount]$targetUser)
Set-Acl -Path $filePath -AclObject $acl
# Disable inheritance and remove existing permissions
$acl.SetAccessRuleProtection($true, $false) # Disable inheritance and remove existing rules
$acl.Access | ForEach-Object { $acl.RemoveAccessRule($_) } # Remove existing access rules
# Grant the specified user Full Control
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($targetUser, "FullControl", "Allow")
$acl.AddAccessRule($accessRule)
# Set the modified ACL back to the file
Set-Acl -Path $filePath -AclObject $acl
Get-Acl -Path $filePath