Category: Powershell

When Group Policy doesn't work, or works wrong
article #1458, updated 13 days ago

If you see GPO policies get implemented and re-implemented even though the settings have been removed, or if it just doesn’t happen, try the following in administrative Powershell. These clear the GPO cache on the machine you’re looking at.

Remove-Item "$env:windir\system32\GroupPolicy" -Force -Recurse
Remove-Item "HKLM:\Software\Policies\Microsoft" -Force -Recurse
Remove-Item "HKCU:\Software\Policies\Microsoft" -Force -Recurse
Remove-Item "HKCU:\Software\Microsoft\Windows\CurrentVersion\Group Policy Objects" -Force -Recurse
Remove-Item "HKCU:\\Software\Microsoft\Windows\CurrentVersion\Policies" -Force -Recurse

Categories:      

==============

Log off all disconnected RDP sessions via Powershell
article #1451, updated 66 days ago

This appears to work well. It uses the olde ‘rwinsta’ command to work around some Powershell oddities.

# Get list of disconnected RDP sessions

$RDPDiscSessions = Get-RDUserSession | Where-Object SessionState -eq STATE_DISCONNECTED

# Disconnect each of them one by one

foreach ($row in $RDPDiscSessions)	{
	'Logging off ' + $row.SessionID
	rwinsta $row.SessionID
}

Categories:      

==============

Simple Powershell Interactive Text-mode "GUI"
article #1450, updated 94 days ago

Rather elegant:

https://spiderzebra.com/2020/05/21/how-to-create-a-simple-powershell-gui/

Categories:      

==============

Download URL Using Powershell
article #1439, updated 137 days ago

Here’s one way:

$wco = (New-Object System.Net.WebClient)
$wco.DownloadFile('https://fqdn/filename.ext','filename.ext')

Categories:      

==============

When Install-Module or Install-PackageProvider Fail
article #1372, updated 622 days ago

An in-script fix:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

A permanent fix:

Set-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\.NetFramework\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord
Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\.NetFramework\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord

Categories:      

==============

Single Download via Powershell
article #1363, updated 671 days ago

To download a single file in binary mode, try translating this:

Invoke-WebRequest -Uri "http://fq.dn/sub/binary.exe" -Outfile "C:\folder\binary.exe"

Categories:      

==============

A CMD line to run a Powershell command
article #1327, updated 851 days ago

Prefix this, add a space to the end, and then type your Powershell:

%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command

Categories:      

==============

Get Version of PowerShell
article #1201, updated 1039 days ago

My current favorite way:

[string]$PSVersionTable.PSVersion.Major + '.' + [string]$PSVersionTable.PSVersion.Minor

If it’s 1.0, you’ll get an error, otherwise you will have what you need.

Categories:      

==============

Set Windows permissions via PowerShell
article #1207, updated 1278 days ago

Here’s a rundown:

https://blogs.msdn.microsoft.com/johan/2008/10/01/powershell-editing-permissions-on-a-file-or-folder/

Categories:      

==============

Run Powershell commands in Labtech/Automate CMD
article #1188, updated 1336 days ago

If you place a tilde (~) before the command you enter, no space after, your command will be run within Powershell. For instance:

~dir

Categories: