Find DotNet Version Using Powershell

I recently faced an issue where one utility tool created by me was not running properly on another machine which had different dot net version installed. During troubleshooting, I was looking for ways to identify the installed dotnet version. Most of the links in google suggested to look for release value in registry as specified here. Below powershell script will list down installed dotnet version on a machine. This is based on dotnet version listed on https://docs.microsoft.com/en-us/dotnet/framework/migration-guide/how-to-determine-which-versions-are-installed. We may have to update below snippet as when new versions are released. Currently it supports upto dotnet 4.7.2 ...

October 3, 2018

Running Powershell Remotely

Code snippet for running power shell on a remote machine. Loosely based on blog post here and here Code below is based on the sample code given in above two links Add reference to System.Management.Automation using System.Management.Automation; using System.Management.Automation.Runspaces; internal void runPowershellRemotely(string location, string scriptToBeRun) { string userName = ConfigurationManager.AppSettings["RemoteMachineLogonUser"]; string password = ConfigurationManager.AppSettings["RemoteMachineUserPassword"]; var securestring = new SecureString(); foreach (Char c in password){ securestring.AppendChar(c); } PSCredential creds = new PSCredential(userName, securestring); // Remove logging if not needed log.Info(String.Format("\tPOWERSHEL : Running Powershell {0} at location {1}", scriptToBeRun, location)); WSManConnectionInfo connectionInfo = new WSManConnectionInfo(); connectionInfo.ComputerName = ConfigurationManager.AppSettings["RemoteMachine"]; connectionInfo.Credential = creds; Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo); runspace.Open(); using (PowerShell ps = PowerShell.Create()) { ps.Runspace = runspace; ps.AddScript(@"cd "+ location); ps.AddScript(scriptToBeRun); try { var results = ps.Invoke(); log.Info("\tPOWERSHEL : Results from Powershell Script is ---------------------------"); foreach(var x in results) { log.Info(x.ToString()); } log.Info("\tPOWERSHEL : End of results--------------------------------- ---------------------------"); } catch (Exception e) { log.Error("\tPOWERSHEL : Exception from running Powershell Script is" + e.ToString()); } } runspace.Close(); }

September 5, 2018

Powershell - Remove entire directory and it's content

The PowerShell command to remove an entire directory and its contents ( including sub folders and files) is below rm -Rf pathToDirectoryToBeRemoved/ R flag denotes to run “rm” command recursively . “f” flag denotes to run in forcefully. We can even replace “f” with “v” for verbose mode and “i” for interactive mode. Note: Above command can also be used to delete files which have long path ( more than 260 characters)

May 2, 2017

Powershell - Copying Folders and Files

Two options for copying files are below. Robocopy - More details can be found at Robocopy Copy_Item cmdlet - More details can be found at Copy-Item Copying Folder structure Only $source = "C:\tools\DevKit2\octopress-blog\source" $dest = "D:\delete" Copy-Item $source $dest -Filter {PSIsContainer} -Recurse -Force #OR robocopy $source $dest /e /xf *.* # /e denotes all folder including empty folders. /xf denotes all files except one of format *.* # /e can be replaced with /s for ignoring empty folders Flattening Folder structure - Copy all files from nested folders to a single folder $source = "C:\tools\DevKit2\octopress-blog\source" $dest = "D:\delete" # Below is required only if we need to create destination folder. Uncomment below line if folder needs to be created #New-Item $dest -type directory Get-ChildItem $source -Recurse | ` Where-Object { $_.PSIsContainer -eq $False } | ` ForEach-Object {Copy-Item -Path $_.Fullname -Destination $dest -Force} Copy same folder structure $source = "C:\tools\DevKit2\octopress-blog\source" $dest = "D:\delete" robocopy $source $dest /e

October 10, 2016