Fix User’s Home Directory Permissions with Take Ownership

February 15th, 2017

Fix-HomeDrivePerms.ps1 is a PowerShell script which attempts to reset folder security when the permissions are really hosed.  It uses a take ownership function, Set-Owner, by Boe Prox, instead of takeown.exe, but does shell out to iCacls.exe.  I wrote this to fix home directories where a user might be logged on with files open a the time, and so it doesn’t rip out the old permissions and replace.

This will require a some editing to run, and this code fragment is set up to do one user folder at a time.  But it might get you going in the right direction.

Script Text

Another User Folder Security Reset Script

February 11th, 2017

This simple batch file resets the inheritance on users folders and then grants them “modify” using the builtin icacls.exe.   This script does not address issues which require you to take ownership — I will post one that does that soon.

I added the “echo” command so you can see what it is doing — remove “echo” when you are ready to run it.

Remember that the variable character for batch files is the percentage sign “%” which must be escaped with a second percentage sign inside a batch file. So if you intend to run this from a command line, you would need to use only a single percentage sign for each variable.

PushD does a temporary drive mapping and changes you to the folder. Popd is the undo for PushD. Both are available inside of PowerShell.

The “FOR” command reads like this: For each directory assign the variable %u.  Run iCacls  to reset security, traversing the folders and continuing  on errors.  The expression %~fu expands %u to a fully qualified path name. The semi-colon allows multiple commands to be stacked. The next iCacls command grants the user modify based on the assumption that the username and folder name are the same.  %~du expands %u to a drive letter only – here, the temporary drive you got from the pushd command.

Variable assignments in the batch for command are case sensitive. If you run “FOR /?” from a command line, you will see a long list of interesting things that the tilde modifier can do with a batch variable.

Set Share Folder Icon for Server 2012 and Windows 10

February 5th, 2017

I like to look at a folder and know whether or not it is shared. In earlier Windows OS there was a hand overlay which shows you this information. In Server 2012, this indicator has been omitted. Set-SharedFolderIcons.ps1 lets you use the icon of your choosing to restore this hint that a folder is shared.   There is a bit at the bottom of the script which you may edit:

The icon is generated with the Icon Extractor, and is used to preview your choice like this:

Confirm Icon Dialog

The script will enumerate shares, skipping admin shares, and, on a domain controller, it skips Sysvol and NetLogon shares.  The list goes to Out-Gridview, and selected shares get the new icon by editing the desktop.ini file.

Script Text


Adding Terminal Services Information to User Reports

February 4th, 2017

Add-ADTSInfo.ps1 adds TerminalServicesHomeDrive, TerminalServicesHomeDirectory, TerminalServicesProfilePath and AllowLogon as additional members returned by a query of Active Directory user objects.  As you may know, when looking at a user’s properties in the Active Directory Users and Computers MMC there is a tab for these fields.  However, if you look at the properties of a user object, these items simply aren’t there.  There are a few articles and scripts addressing this problem, and you will find that the only way to get the data is by binding to each individual user object and using the a method like this: $ADSIUser.psbase.InvokeGet(‘TerminalServicesProfilePath’).

My script differs from others in that you can pipe an object containing user objects with any properties, and it will add the fields listed above to your results.  I added sorting of the new resulting so that the property names are in order.  This is an advanced function with comment based help.

Script Text

Combine CSV Files to XLSX File with Worksheets

February 4th, 2017

Three years ago, I posted CombineXLSheets.vbs, a vbScript which allows you to drop and drag Excel spreadsheets onto it and have them combined into a single workbook file with multiple worksheets.  Now that I am working in PowerShell, I have found the need to consolidate CSV files.  Parts of  Combine-CSVFilesToWorksheets.ps1 come from code ported from the vbscript, and this script does a similar job.

I have been experimenting with input validation choices, and you can see some of it here.  One of the things I learned is that a single parameter can have more than one script validations using ValidateScript.  I also decide that the error handling for this is pretty ugly, and moved other input validation into the body of the script.

The script requires that you specify the folder with the CSV files, and the output file for the combined data.  By default the worksheets are named with the base name of the CSV file, and autofit is applied to the columns.  You may optionally turn off autofit, and can delete all of the CSV files when done or the CSV folder itself.  Because this is an advanced function, you can use Get-Help for full help.  The script also supports ShouldProcess (-WhatIf ).

This script requires Excel.

Script Text

Consolidating Data For Report: Getting from Group-Object to CSV

February 4th, 2017

I am working on some scripts which show AD domain controllers by their site.  My goal was to have the data combined so that  each site has a single row, with the server name and IP address for each site being shown joined by semi-colons.






None of the examples for doing this worked for me. But this did:

Export to XLSX without Excel: Export-XLSX.ps1 Revisited and Tweaked

January 30th, 2017

I often look at the code of others with respect and admiration. Peter Kriegel (Germany) wrote Export-XLSX.ps1, an amazing script which lets you export data into a real XLSX file without Excel being installed. The script also enables you to directly append worksheets to XLXS files. His website,, hasn’t been updated in a while, and the automatic translate function is broken. It isn’t clear to me how to reach Peter, as I typically don’t post the code of others with so few changes, so I want to be clear — this isn’t my work.

I converted the original script to an advanced function, and made sure the Help was functioning right.  The only limitation is with formatting, which means it will look pretty much like a file from Export-CSV.  Nonetheless, if you have scripts running on servers where you really don’t want to install Excel, this is an excellent function to use.  The script is heavily commented and has examples in the help.

Script Text

Get the NetBIOS AD Domain Name from the FQDN

January 30th, 2017

I hate using NameTranslate, because it is a COM object, and because the output is often really hard to get into a clean, trimmed string.  The netBIOS name isn’t a part of the AD domain object, but I suspected that the information could be gotten using a LDAP query.  My searching lead me to a post on StackFlow.  It wasn’t PowerShell, but it did give me an interesting hint.  The filter’s objectcategory was “CrossRef”.  I used this to port the code to PowerShell:

This query is quick, and avoids the formatting problems with NameTranslate.  There is a large table of LDAP queries on TechNet, but this one isn’t in the list.

Convert System.DirectoryServices.SearchResult to a PSObject

December 25th, 2016

The ADSI accelerator is fast, and built into PowerShell, unlike the Active Directory Module.  When use it, or the ADSISearcher, you have results which look like this [Image from previous Microsoft URL]:

FindAll Results

Getting the properties out to a file can be tricky.  I wrote two little functions to make this easier:

Get-AllDNSServersInForest.ps1, demonstrates how to use these functions.  It uses the ADSI accelerator to create the ADSI Searcher, then  sends a list of all DNS servers in the forest to Out-Gridview, by using the query “(servicePrincipalName=DNS*)”
Script Text

Get COM Object Constants with PowerShell

December 25th, 2016

I avoid using COM objects whenever I can. One of the annoyances is that you need to look up the value of constants. Shay Levy, in has a script which demonstrates how to enumerate the constants for the Excel COM object.  Get-COMConstants.ps1, is an advanced function generalization, which can be used for any COM object.  The function contains a number of examples, such as this, which shows the constant names and values, and sends the selected constant value to the clipboard.

Script Text