Alan's Blog

"Yeah. I wrote a script that will do that."

Menu
  • About My Blog
Menu

Tag: Powershell Scriptlets

Get WMI Namespaces with PowerShell

Posted on February 28, 2019 by Alan

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…

Read more

PowerShell Get Column Names for a CSV File

Posted on August 13, 2017 by Alan

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

Read more

Powershell Date LDAP filters

Posted on August 13, 2017 by Alan

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  

Read more

PowerShell Pause with Progress Bar

Posted on August 13, 2017 by Alan

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…

Read more

Fix Creation Date Later than Date Modified with PowerShell

Posted on August 13, 2017 by Alan

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…

Read more

OU of Current PC from anywhere in the Forest

Posted on August 12, 2017September 23, 2017 by Alan

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…

Read more

An Empty Pipe Element is not Allowed Here – Workaround

Posted on August 12, 2017 by Alan

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 })…

Read more

Split a List and Remove Empty Lines with PowerShell

Posted on August 12, 2017 by Alan

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.

Read more

Export to HTML with Style Sheet Header

Posted on February 25, 2017 by Alan

  $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…

Read more

A Dot Source Reminder for Advanced Functions

Posted on February 25, 2017April 2, 2017 by Alan

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…

Read more
  • 1
  • 2
  • Next

Search

Please Note

All the scripts are saved as .txt files. Newer files have a “View Script” button which will let you save or open a script in notepad. For earlier posts, the easiest way to download with IE is to right click on the link and use “Save Target As”. Rename file from Name_ext.txt to Name.ext.

To see a full post after searching, please click on the title.

PowerShell Scripts were written with version 3 or later.

https connections are supported.

All new users accounts must be approved, as are comments. Please be patient.  If you find a post error or a script which doesn’t work as expected, I appreciate being notified.  My email is my first name at the domain name, and you are welcome to contact me that way.

Tags

1E ACLS Active Directory ActiveDirectory ADSI Advanced Functions Audit Change Administrator Password COMObject Computer Groups DateTime Desktop DNS Excel FileScriptingObject Forms General GPO GPS Group Policy Hacks ISE Lockout logons NAV740 Nessus OU OU permissions Outlook Pick Folder Power PowerShell Powershell Scriptlets RDP SCCM schedule reboot Scripting Security Shell.Application user information VBA Windows Update WMI WordPress WPF

Categories

akaplan.com

  • Back to Home Page

Archives

Scripting Sites

  • A Big Pile of Small Things
  • Adam, the Automator
  • Art of the DBA
  • Ashley McGlone
  • Boe Prox
  • Carlo Mancini
  • DexterPOSH
  • Doug Finke
  • Jaap Brasser's Blog
  • JeffOps The Scripting Dutchman
  • Jonathan Medd's Blog
  • Keith Hill's Blog
  • LazyWinAdmin
  • Nana Lakshmanan
  • PowerShell Magazine
  • PowerShell Team Blog
  • PowerShell.org
  • PwrShell.net
  • Richard Siddaway's Blog
  • Ryan Yates' Blog
  • Skatterbrainz
  • The Lonely Administrator

SQL Site

  • Art of the DBA

Meta

  • Register
  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
©2025 Alan's Blog | Theme by SuperbThemes

Terms and Conditions - Privacy Policy