• /r/powershell
  • Could anyone help explain this (it’s probably really simple…)?

    I was trying to strip whitespace from the end of each line of output when running this command: Get-ADPrincipalGroupMembership -identity username | Select-Object -name In the end after much messing around, I came across this command which did the job for me: Get-ADPrincipalGroupMembership -identity username | Select-Object -ExpandProperty name There’s an explanation on the page […]

  • /r/powershell
  • command to return empty directories returns non-empty directories / how stop next line until cmdlet completes

    I need to scrap through all our shares and find any empty directories so I’m using this: $shares = “Office Operations”, “Training” foreach ($share in $shares) { $dirs = Get-ChildItem -Path “E:backup$share” -Recurse -Force | where {$_.PSIsContainer -eq $True} $emptyDirs = $dirs | where {$_.GetFiles().Count -eq 0} | select FullName } # to display output […]

  • /r/powershell
  • Here is a quick script to add users to local power users and remote desktop. Compatible with all versions of server that has remote commands enabled

    function get-localadmin { param ([parameter(mandatory=$true)]$computer,[parameter(mandatory=$true)]$user ) $domainuser= “<enter your domain here>$user” Write-Output “Adding $user now!” invoke-Command $computer -scriptblock { $user=$args[0] $nullout=net localgroup “power users” /add “$user” $nullout=net localgroup “remote desktop users” /add “$user” $power= net localgroup “power users” $power=$power| ?{$_ -eq “$user”} $remote= net localgroup “remote desktop users” $remote=$remote| ?{$_ -eq “$user”} if ($power){ write-host […]