Permanently setting Windows environment variables from scripts (e.g. PowerShell)

To permanently set Windows environment variables (e.g. PATH) from a (PowerShell) script, you can use setx or Set-ItemProperty like this (for the current user): setx VARIABLE value Set-ItemProperty -Path ‘Registry::HKEY_CURRENT_USER\Environment’ -Name VARIABLE -Value “value” And here is the result:

PowerShell: Configure some basic Windows settings after a fresh installation

After installing Windows I always started configuring the same old basic settings: move Desktop, Documents, Music, Videos, Pictures folders to D:\ configure some basic settings in Windows Explorer folder options (e.g. show hidden files, show file extensions) remove Internet Explorer from taskbar and add PowerShell to it Today I set up a few virtual machines […]

Git ignores .gitignore when working with PowerShell

Today, a Git problem drove me nuts: I added files to the .gitignore file to make sure Git does not track and commit them to the repository. However, Git stubbornly ignored the file and kept adding all the files in my directory to the index. As it turned out, PowerShell was the problem. I added […]

PowerShell: Check whether a Windows command or executable is available

In one of my recent PowerShell scripts I needed to find out if certain commands (in my case “svn”, “git”, “ant” and “mvn”) were available or not. I wrote this small function that tries to call the given command and returns whether it is callable. function commandAvailable($cmd, $options) {   $error.clear();   $ErrorActionPreference = "silentlycontinue"; […]

PowerShell: How to read contents from text files without converting the lines into an array

Apparently, PowerShell’s get-content cmdlet automatically converts the contents of text files into an array of strings, splitting the contents at the line breaks (see Using the Get-Content Cmdlet). In many cases this is convenient: Contents of file test.txt: 1 2 3 gc text.txt | % { write-host “line: ” $_ } line: 1 line: 2 […]

Running PowerShell scripts from a UNC path without security warnings

Today I tried running a PowerShell script from a UNC path on a Windows 2003 server machine. Although I set the execution policy to “Unrestricted”, a security warning came up asking me whether I really wanted to execute the script: I found the solution to this problem here: Running Scripts Downloaded from the Internet in […]

Verweis auf mein PowerShell-Script in der aktuellen Ausgabe der hakin9

In der aktuellen Ausgabe der hakin9 verweist Hannes Schurig im Rahmen seines Artikels “Manuelle und automatisierte Administration einer Active Directory Domäne und Grundlagen der Gruppenrichtlinien” auf mein kleines PowerShell-Script zum Auslesen von Computern aus einem Active Directory. Danke für die Referenz! Aber unabhängig von der kleinen Referenz hat Hannes einen interessanten Einführungsartikel in die automatische […]

web2bibtex.ps1: BibTeX-Einträge für Onlineartikel aus Fachzeitschriften generieren

Ich pflege mit Zotero eine Liste aller von mir gelesenen Fachartikel. Dazu ist es notwendig, die Metadaten dieser Artikel zu erfassen. Da ich jedoch wenig Spaß daran habe, das für mehrere Artikel pro Woche manuell zu tun, habe ich mir ein kleines PowerShell-Script geschrieben, das aus den Beschreibungen der Artikel auf den Websites der Zeitschriften […]

PowerShell: Read user’s groups from Active Directory

Here’s a short PowerShell function to retrieve a list of a user’s groups from Active Directory: function getADGroups($username) {   $root = ([adsi]"").distinguishedName;   $searcher = new-object System.DirectoryServices.DirectorySearcher($root);   $searcher.filter = "(&(objectClass=user)(sAMAccountName=$username))";   $user = $searcher.findall();     $groupNames = @();   if ($user.count -eq 1)   {     $groups = $user[0].Properties.memberof;     […]

PowerShell: Uninstall programs on a remote PC

Today I wanted to roll out Microsoft Office 2010 to a few clients but I needed to get rid of old Office installations on these systems because those could lead to problems while installing the new version. Instead of uninstalling them manually I wrote the following short PowerShell script UninstallOffice.ps1 for the task, which takes […]