There will be situations where you will need to install applications silently without any human intervention. This is a pretty normal use case for System Administrators and they love to automate their tasks. In fact knowledge of this subject may be handy for any technology worker in various circumstances. In this article we will silently install FileZilla on to the computer. Before we proceed further, here are a few things to keep in mind:

Scenario: In my case here I have the installer exes in "ApplicationRepository" folder in C drive as shown below.

ApplicationRepository

We will only be installing FileZilla as of now but you can extend it for any number of applications as you wish.

Also let us make sure that FileZilla is not installed by looking at installed applications in Control Panel. As we can see below, FileZilla is not currently installed.

FileZilla

Now let us see the code which does the magic i.e. installs application silently.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Management.Automation;
  9. namespace SilentInstallation
  10. {
  11. class Program
  12. {
  13. static void Main(string[] args)
  14. {
  15. ProcessFolder();
  16. }
  17. private static void ProcessFolder()
  18. {
  19. const string SOURCEFOLDERPATH = @ "C:\ApplicationRepository";
  20. if (Directory.Exists(SOURCEFOLDERPATH)) {
  21. Console.WriteLine("Directory exists at: {0}", SOURCEFOLDERPATH);
  22. if (Directory.GetFiles(SOURCEFOLDERPATH, "*.exe").Length > 0) {
  23. int count = Directory.GetFiles(SOURCEFOLDERPATH, "*.exe").Length;
  24. string[] files = Directory.GetFiles(SOURCEFOLDERPATH, "*.exe");
  25. foreach(var file in files) {
  26. var fileName = System.IO.Path.GetFileName(file);
  27. var fileNameWithPath = SOURCEFOLDERPATH + "\\" + fileName;
  28. Console.WriteLine("File Name: {0}", fileName);
  29. Console.WriteLine("File name with path : {0}", fileNameWithPath);
  30. //Deploy application
  31. Console.WriteLine("Wanna install {0} application on this VM?
  32. Press any key to contiune.
  33. ", fileName);
  34. Console.ReadKey(); DeployApplications(fileNameWithPath); Console.ReadLine();
  35. }
  36. }
  37. } else
  38. Console.WriteLine("Directory does not exist at: {0}", SOURCEFOLDERPATH);
  39. }
  40. public static void DeployApplications(string executableFilePath)
  41. {
  42. PowerShell powerShell = null;
  43. Console.WriteLine(" ");
  44. Console.WriteLine("Deploying application...");
  45. try {
  46. using(powerShell = PowerShell.Create())
  47. {
  48. //here “executableFilePath” need to use in place of “
  49. //'C:\\ApplicationRepository\\FileZilla_3.14.1_win64-setup.exe'”
  50. //but I am using the path directly in the script.
  51. powerShell.AddScript("$setup=Start-Process 'C:\\ApplicationRepository\\
  52. FileZilla_3 .14 .1 _win64 - setup.exe ' -ArgumentList ' / S ' -Wait -PassThru");
  53. Collection < PSObject > PSOutput = powerShell.Invoke(); foreach(PSObject outputItem in PSOutput) {
  54. if (outputItem != null)
  55. {
  56. Console.WriteLine(outputItem.BaseObject.GetType().FullName);
  57. Console.WriteLine(outputItem.BaseObject.ToString() + "\n");
  58. }
  59. }
  60. if (powerShell.Streams.Error.Count > 0)
  61. {
  62. string temp = powerShell.Streams.Error.First().ToString();
  63. Console.WriteLine("Error: {0}", temp);
  64. } else
  65. Console.WriteLine("Installation has completed successfully.");
  66. }
  67. }
  68. catch (Exception ex)
  69. {
  70. Console.WriteLine("Error occured: {0}", ex.InnerException);
  71. //throw;
  72. } finally
  73. {
  74. if (powerShell != null)
  75. powerShell.Dispose();
  76. }
  77. }
  78. }
  79. }

Now let us see this code in action using the following screenshots:



Deploying/ Installing Application



Application

Depending upon the User Control Settings, there may be a prompt to allow our App to install another app(i.e. FileZilla). If you don't want this pop up and want absolute silent installation, change your settings to Never Notify as shown below and again run the console App if required after changing the setting. (Please Note: This setting is not recommended and it's good to revert after the demo).

uac

If everything goes well, the application will be installed successfully as shown below.

application

Now again go to the Control Panel and refresh the list of installed applications. Now you will be able to see FileZilla installed on your machine.

FileZilla


In the next version I will try to write a WCF service that can run on a Windows Server and you can hit it with a POST request with relevant information to install an application when required. This concept can be used at many places especially in Server Maintenance and Application Installation.

You can download the above source code and screenshots by downloading the attached zip file.

Hope you enjoyed the article.

Thank You.

Read more articles on C# Programming: