Here is a quick way to get a list of WMI namespaces on a computer using PowerShell. Notice that this requires that you run it as an administrator. #Requires -RunAsAdministrator Function Get-WMINamespaceEnum ($NS) { Write-Output $ns Get-CimInstance “__Namespace” -Namespace $NS -ErrorAction SilentlyContinue | ForEach-Object { Get-WMINamespaceEnum “$ns\$($_.name)” } } #Example Get-WMINamespaceEnum ‘root’ | Sort-Object The…
Tag: Powershell Scriptlets
PowerShell Get Column Names for a CSV File
Get-Member doesn’t always show you what is under the hood for an object. For that you need the .PSObject property. Here PSObject.Properties contains CSV column names $data = Import-Csv -Path $csvfile $ColNames = ($data[0].psobject.Properties).name $ColNames
Powershell Date LDAP filters
This snippet can be used for easier date formatting when using an LDAP date filter with PowerShell. This demonstrates how to get users created within the previous 30 days using LDAP: $MaxDays = 30 $StartDate = (Get-date).AddDays(-$MaxDays) #Set to begin at midnight $ldapStart = $StartDate.GetDateTimeFormats()[5].ToString().Replace(“-“,”)+’000000.0Z’ $LDAPFilter = “(WhenCreated>=$ldapStart)” Get-aduser -LDAPFilter $ldapfilter -properties whencreated
PowerShell Pause with Progress Bar
This snippet of PowerShell was written to have show users something more interesting than “Sleeping for 15 seconds” in a script. Notice that I splat the progress parameters. #The math works only with seconds $pauseSecs = 1 $MaxWaitSecs =15 for ($i=0; $i -lt $MaxWaitSecs; $i+=$pauseSecs) { [int]$pct = ($i/$MaxWaitSecs)*100 $Params = @{ Activity = “Please…
Fix Creation Date Later than Date Modified with PowerShell
A weird and annoying thing happened to my home directory at work when it was moved from Windows to a storage appliance. The file CreationTime was lost on all the files and was set to the date of the data move. Particularly annoying was seeing the CreationTime being more recent than the LastWriteTime attribute. At…
OU of Current PC from anywhere in the Forest
There are a lot of ways to get the OU of the current computer, but most don’t work if you are outside your home domain. This code does, without requiring AD cmdlets: #My Computername works anywhere in forest $strFilter = “(&(objectCategory=Computer)(Name=$env:computername))” $objSearcher = New-Object System.DirectoryServices.DirectorySearcher $objSearcher.Filter = $strFilter $searchRootName = [system.directoryservices.activedirectory.forest]::GetCurrentForest().Name.ToString() $SearchRoot = “GC://”+$SearchRootName $objSearcher.SearchRoot…
An Empty Pipe Element is not Allowed Here – Workaround
This code gives you the error, “An Empty Pipe Element is not Allowed Here”: $files = Get-ChildItem foreach ($file in $files) {$file.FullName } | Out-GridView -title “Example” The workaround which solves this problem is to make it an array by enclosing the code in @(), Example: $files = Get-ChildItem @(foreach ($file in $files) {$file.FullName })…
Split a List and Remove Empty Lines with PowerShell
I often have lists where I have to split the list, and remove empty lines. This is how I do it: $Properties =@” HostName TaskName Task To Run Start Date Start Time “@ $Properties.Split(“`r`n|`r|`n”,[System.StringSplitOptions]::RemoveEmptyEntries) This method uses a regular expression with three different variations of line break, then the .NET method of removing empty lines.
Export to HTML with Style Sheet Header
$txtColor = ‘AliceBlue’ $file = “$env:temp\TempHTML.htm” $HTMLHeader =@” <style> BODY{background-color:white;} TABLE{border-width: 1px;border-style: solid;border-color: black; border-collapse: collapse;margin-left:auto; margin-right:auto;} TH{border-width: 1px;padding: 1px;border-style: solid; border-color: black;background-color:$txtColor} TD{border-width: 1px;padding: 1px;border-style: solid; border-color: black;background-color:$txtColor} </style> “@ #Example $title= ‘Service Information’ Get-Service | Select-Object Status, Name, DisplayName,Starttype | ConvertTo-HTML -head $HTMLHeader -body “<center><H2>$title</H2><Font=Verdana></Center>” | Out-File $file Invoke-Expression $file This a…
A Dot Source Reminder for Advanced Functions
One of the problems with writing advanced functions is that new PowerShell users think that they don’t do anything. Frankly, I couldn’t figure out a way to get a notification to work, so I reached out to the sponsor for the Charlotte PowerShell User Group, Microsoft PFE Brian Wilhite. Brian sent me some code which…