Special variables declarion and saving output in vb.net

~OSD~ 2,196 Reputation points
2020-12-01T11:56:06.183+00:00

Hi,

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 "
        Process.Start(get_AppxList)
    End Sub

Any thoughts how to trick with this?

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,921 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,737 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,553 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 85,881 Reputation points
    2020-12-02T11:01:58.727+00:00

    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
    p.Start()
    p.WaitForExit()
    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
    2020-12-01T14:40:33.017+00:00

    Hi
    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 37,706 Reputation points Microsoft Vendor
    2020-12-01T14:40:58.873+00:00

    Hi,

    Please try $env:UserName instead.

    Best Regards,
    Ian

    ============================================

    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,196 Reputation points
    2020-12-02T09:11:57.217+00:00

    Hi,

    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,196 Reputation points
    2020-12-02T13:34:08.2+00:00

    Thanks Castorix31

    I tried it but no output were saved or no errors were thrown etc.
    44398-image.png

    44405-image.png


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.