Skip to content

Instantly share code, notes, and snippets.

@Diagg
Diagg / Invoke-ScheduledTask.ps1
Created November 15, 2021 00:05
This function will help showing user interface when using client in system context like SCCM, Intune or Workspace One.
Function Invoke-ScheduledTask
{
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[String]$TaskName,
[Parameter(Mandatory = $true)]
[String]$command,
[Parameter(Mandatory = $false)]
[string]$Parameters,
@Diagg
Diagg / Parse-BCDEdit.ps1
Created October 25, 2021 23:32
Parse BCDedit enumation to a Powershell Object
## Parse BCDedit enumation to a Powershell Object
## By Diagg/OSDC - 26/10/2021
## Usage exemple: $Bcdedit|where Index -eq 0|select default
$ObjName = $null ; $Val = 0 ; $Index = 0 ; $Bcdedit = @()
$BcdString = bcdedit /v
$BcdString|Foreach {
If ($_.startswith('-'))
{
@Diagg
Diagg / OSFriendlyBuild.ps1
Created October 12, 2021 22:08
how to retrieve meaningful (like 21H2) build number from the internet. (Support Windows 10 and 11)
# OSFriendlyBuild.ps1 by Diagg/OSD-Couture.com
$OSBuild = @{}
$BuildNumber = (Get-ItemProperty 'HKLM:SOFTWARE\Microsoft\Windows NT\CurrentVersion').CurrentBuild
IF ([int]($BuildNumber) -lt 22000){$HTML = Invoke-RestMethod 'https://docs.microsoft.com/en-us/windows/release-health/release-information'}
else {$HTML = Invoke-RestMethod 'https://docs.microsoft.com/en-us/windows/release-health/windows11-release-information'}
$Pattern = '<strong>(?<version>.*)<\/strong>'
$AllMatches = ($HTML | Select-String $Pattern -AllMatches).Matches
($AllMatches.Groups | Where-Object {$_.name -eq 'version'}).value -replace "Version " -replace "\(RTM\) " -replace "\(original release\) " -replace "\(OS build" -replace "\)"| ForEach-Object {$Htbl = $_ -split " "; $OSBuild[$Htbl[2]] = $Htbl[0]}
Try {[System.Environment]::SetEnvironmentVariable('OSBUILD',$OSBuild[$buildnumber],'Machine')}
@Diagg
Diagg / Convert-DsRegCmd.ps1
Last active September 7, 2022 08:55
Convert DsRegCmd to a Powershell object with a oneliner
# Inspiration from https://gist.github.com/d4rkeagle65/b9bc42a26be44a6d66c4858a4c3bc944 by d4rkeagle65
# Output to object by Diagg/OSD-Couture.com
$Dsregcmd = New-Object PSObject ; Dsregcmd /status | Where {$_ -match ' : '}|ForEach {$Item = $_.Trim() -split '\s:\s'; $Dsregcmd|Add-Member -MemberType NoteProperty -Name $($Item[0] -replace '[:\s]','') -Value $Item[1] -EA SilentlyContinue}
# to vue full objest type : $Dsregcmd
# to vue property TenantID : $Dsregcmd.TenantId
@Diagg
Diagg / GetCurrentUser.ps1
Last active September 25, 2023 17:52
Get Current user from system/admin context. Works with Workgroup/AD user, Windows Sandbox user and Azure AD user. Works also with an x86 Powershell client on an X64 Windows 10
# By Diagg/OSDC
# https://www.osd-couture.com/
# Twitter: @Diagg
#V 2.0 - Logic refactored, Added support for X86 powershell on X64 windows
#V 1.0 - Initial release
# Get Workgroup/AD User
$CurrentLoggedOnUser = (Get-CimInstance –ClassName Win32_ComputerSystem | Select-Object -expand UserName)
If ([String]::IsNullOrWhiteSpace($CurrentLoggedOnUser))
@Diagg
Diagg / UnPDFEdge.Ps1
Last active May 3, 2021 08:13
Various untested trick to prevent edge from hijacking the PDF viewer
# Stops edge from taking over as the default .PDF viewer
Write-Output "Stopping Edge from taking over as the default .PDF viewer"
# Identify the edge application class
$Packages = "HKCU:SOFTWARE\Classes\Local Settings\Software\Microsoft\Windows\CurrentVersion\AppModel\Repository\Packages"
$edge = Get-ChildItem $Packages -Recurse -include "MicrosoftEdge"
# Specify the paths to the file and URL associations
$FileAssocKey = Join-Path $edge.PSPath Capabilities\FileAssociations
@Diagg
Diagg / RunScriptBlockAsTrustedInstaller.ps1
Last active July 11, 2024 23:17
Run Powershell Script block as Trusted installer using Scheduled Task under Admin account
# Run Powershell scriptblock as Trusted Installer From Admin context (Yeah, MDT) using Scheduled Task.
# Credit due to : https://www.tiraniddo.dev/2019/09/the-art-of-becoming-trustedinstaller.html
$ScriptBlock = {
$Script:TsEnv = New-Object PSObject
$Script:TsEnv|Add-Member -MemberType NoteProperty -Name 'SystemHostName' -Value ([System.Environment]::MachineName)
$Script:TsEnv|Add-Member -MemberType NoteProperty -Name 'SystemIPAddress' -Value (Get-NetIPAddress -AddressFamily IPv4 -PrefixOrigin Dhcp -AddressState Preferred).IPAddress
$Script:TsEnv|Add-Member -MemberType NoteProperty -Name 'SystemOSversion' -Value ([System.Environment]::OSVersion.VersionString)
@Diagg
Diagg / RunASTrustedInstaller.ps1
Last active February 20, 2025 09:20
Run task as Trusted installer using Scheduled Task under system account
# Run task as Trusted Installer From system context (Yeah, Intune, SCCM)
# Should also work under Admin context (if not, remove all references to $P).
# Credit due to : https://www.tiraniddo.dev/2019/09/the-art-of-becoming-trustedinstaller.html
$a = New-ScheduledTaskAction -Execute notepad.exe
$P = New-ScheduledTaskPrincipal -GroupId "BUILTIN\Administrateurs"
#$P = New-ScheduledTaskPrincipal -GroupId "BUILTIN\Administrators" #Warning: the admin Group name is localised
Register-ScheduledTask -TaskName 'TestTask' -Action $a -Principal $P
#########################
#
# Set-DefaultAppAsociation
#
# Version 3.0 By Diagg/OSD-Couture.com
# XML node creation part by Helmut Wagensonner (https://blogs.msdn.microsoft.com/hewagen/making-file-type-associations-enterprise-ready/)
# Release Date 01/10/2019
# Latest relase: 06/01/2020
#
# Purpose: Create file association on the fly during MDT/SCCM deployment
@Diagg
Diagg / GUI-V2.ps1
Last active April 15, 2019 14:44
This is another example on using Show-command to create simple user interface
Function Install-WEBService
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$true,position=0)]
[String]${_0_User},
[Parameter(Mandatory=$true,position=1)]
[String]${_1_Password},