Change Drive Letter

From current letter D: to Z:

Set-WmiInstance -InputObject ( Get-WmiObject -Class Win32_volume -Filter "DriveLetter = 'd:'" ) -Arguments @{DriveLetter='Z:'}

Shrink WinSxS Folder

Get-WindowsFeature | where-object{$_.Installed -eq 0 -and $_.InstallState -eq 'Available'} | uninstall-windowsfeature -remove

Get/Create Registry Keys or Items

Get Item(s)

Get-ItemProperty 'Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\...' | fl

Create New Key/Folder

New-Item -Path 'Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOME\Path\Create' -Force | Out-Null

Create New Item:

New-ItemProperty -Path 'Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOME\Path\Create\' -Name MyVar -Value 1 -PropertyType DWORD -Force | Out-Null

Set Environment Vairables

[Environment]::SetEnvironmentVariable("THOR", "himself", "Machine")
  1. Variable Name
  2. Value
  3. Scope: User or Machine

To see such changes you need to start a new Powershell process

> Get-ChildItem env:

# or

> Get-ChildItem env:THOR

# or

> Get-ChildItem env:THO*

Service Principal Name (SPN)

Query SPNs

setspn -T europe -F -Q */backup.domain.tld

List SPNs

setspn -L <accountname>

setspn -L <hostname>

Register SPN
It will register SPN “HOST/server” and “HOST/{DNS of server}”

setspn -R <server>

Register additional SPN (alias) for <server>

setspn -S host/<serveralias> <server>

Delete server alias

setspn -D host/<serveralias> <server>

Register MsSQLsrv SPN. (You need to register both <server> and <server>:1433)

setspn -S MsSQLsvr/<server> <server>

setspn -S MsSQLsvr/<server>:1433 <server>

List Shared Folders

get-WmiObject -class Win32_Share 

set/remove ACL folder permissions

Inheritance on

foreach ($folder in Get-ChildItem -Path .\Programme -Recurse -Directory) {
   $AccessRule = New-Object System.Security.Accesscontrol.FileSystemAccessRule ("domain\user", "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow")
   $acl = Get-Acl $folder.fullname
   $acl.SetAccessRuleProtection($false, $true)  # Inheritance on
   $acl.SetAccessRule($AccessRule)
   Set-Acl -Path $folder.FullName -AclObject $acl
}

Inharitance off

foreach ($folder in get-item \\<server>\e$\Folder) {
   $AccessRule = New-Object System.Security.Accesscontrol.FileSystemAccessRule ("domain\user", "ListDirectory", "None", "None", "Allow")
   $acl = Get-Acl $folder.fullname
   $acl.SetAccessRuleProtection($true, $false)  # Inheritance off
   $acl.SetAccessRule($AccessRule)
   Set-Acl -Path $folder.FullName -AclObject $acl
}

Remove a User/Group completely from ACLs
(This includes all Allow AND Deny rules)

$acl = Get-Acl D:\path
$usersid = New-Object System.Security.Principal.Ntaccount("DOMAIN\user")
$acl.PurgeAccessRules($usersid)
$acl | Set-Acl D:\path

Copy File Permissions

Copy file permissions from E:\data to F:\data

cd e:
icacls .\Data /save .\DATA_perms.txt /c /t
icacls F: /restore E:\DATA_perms.txt /c

where:

/t     Traverse through folders
/c Continue on errors

Create Shortcut

$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$Home\Desktop\NAME.lnk")
$Shortcut.TargetPath = "C:\Program Files (x86)\ColorPix\NAME.exe"
$Shortcut.Save()

Leave a Reply