Special variables declarion and saving output in vb.net

~OSD~ 2,131 Reputation points


I am using following code but stuck with variable declaration %UserName% and saving output in text file with ">"

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
         Dim get_AppxList As New ProcessStartInfo("PowerShell")
        get_AppxList.Arguments = "Get-AppxPackage -ErrorAction SilentlyContinue " > " C:\Users\**%UserName%**\AppsList\InstalledAppxPackagesList.txt "
    End Sub

Any thoughts how to trick with this?

Microsoft Technologies based on the .NET software framework.
3,549 questions
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,637 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,435 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 82,576 Reputation points

    You can do something like =>

    Dim sUserPath As String = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
    sUserPath += "\AppsList\InstalledAppxPackagesList.txt"
    Dim p As Process = New Process()
    p.StartInfo.FileName = "PowerShell.exe"
    p.StartInfo.Arguments = "Get-AppxPackage -ErrorAction SilentlyContinue > " + sUserPath
    p.StartInfo.UseShellExecute = False
    p.StartInfo.CreateNoWindow = True
    p.StartInfo.RedirectStandardError = True
    Dim sStdErr As String = p.StandardError.ReadToEnd()
    Console.WriteLine("Exit code : {0}", p.ExitCode)
    Console.WriteLine("StdErr : {0}", sStdErr)
    2 people found this answer helpful.
    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Anonymous

    Here is one way to get a well formed string with the path I think you want. The effect/results not checked though.

    Dim s As String = "Get-AppxPackage -ErrorAction SilentlyContinue > " & IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "AppsList\InstalledAppxPackagesList.txt")
    0 comments No comments

  2. Ian Xue (Shanghai Wicresoft Co., Ltd.) 32,326 Reputation points Microsoft Vendor


    Please try $env:UserName instead.

    Best Regards,


    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  3. ~OSD~ 2,131 Reputation points


    Both options didn't worked for me.

    If in CMD/ Command Prompt I am able to run following command successfully but seems like can't translate it to work with VB.
    PowerShell Get-AppxPackage -ErrorAction SilentlyContinue > C:\Users\%UserName%\AppsList\InstalledAppxPackagesList.txt

    0 comments No comments

  4. ~OSD~ 2,131 Reputation points

    Thanks Castorix31

    I tried it but no output were saved or no errors were thrown etc.