title: PowerShell - script 1 calls script 2 - how to return value from script 2 to script 1
body: <p>I have two PowerShell scripts. One script invokes another PowerShell script using elevated credentials, using <a href="https://technet.microsoft.com/en-us/library/hh849848.aspx" rel="nofollow noreferrer">Start-Process</a>.</p>
  
  <p>But I am struggling with how to make the second script return the output value to the first script.</p>
  
  <p>Here is script # 1, which is invoked with script1.psl "sender-ip=10.10.10.10"</p>
  
  <pre><code>function getUser($abc) {
  
      &lt;#Previously created password file in C:\Script\cred.txt, read-host -assecurestring | convertfrom-securestring | out-file C:\Script\cred.txt#&gt;
  
      $password = get-content C:\Script\cred.txt| convertto-securestring
      $credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist "DOMAIN\Username",$password
      $output = start-process powershell -Credential $credentials -ArgumentList '-noexit','-File', 'C:\script\script2.ps1', $abc
      return $output
  }
  
  [string]$abc = $args
  
  getUser($abc)
  
  Write-host Output is $output
  </code></pre>
  
  <p>When I execute the script, the output is</p>
  
  <pre><code>Output is
  </code></pre>
  
  <p>And here is script2, which outputs the desired value to a cmd window instead of returning the value to script 1:</p>
  
  <pre><code>$userID = $NULL
  $line_array = @()
  $multi_array = @()
  [hashtable]$my_hash = @{}
  
  foreach ($i in $args) {
     $line_array+= $i.split(" ")
  }
  
  foreach ($j in $line_array) {
      $multi_array += ,@($j.split("="))
  }
  
  foreach ($k in $multi_array) {
      $my_hash.add($k[0],$k[1])
  }
  
  $Sender_IP = $my_hash.Get_Item("sender-ip")
  
  
  &lt;# Courtesy of http://blogs.technet.com/b/heyscriptingguy/archive/2012/02/19/use-powershell-to-find-last-logon-times-for-virtual-workstations.aspx#&gt;
  
  &lt;# Gather information on the computer corresponding to $Sender_IP #&gt;
  $Win32OS = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $Sender_IP
  
  &lt;# Determine the build number #&gt;
  $Build = $Win32OS.BuildNumber
  
  
  &lt;# Running Windows Vista with SP1 and later, i.e. $Build is greater than or equal to 6001 #&gt;
  if ($Build -ge 6001) {
      $Win32User = Get-WmiObject -Class Win32_UserProfile -ComputerName $Sender_IP
      $Win32User = $Win32User | Sort-Object -Property LastUseTime -Descending
      $LastUser = $Win32User | Select-Object -First 1
      $UserSID = New-Object System.Security.Principal.SecurityIdentifier($LastUser.SID)
      $userId = $UserSID.Translate([System.Security.Principal.NTAccount])
      $userId = $userId.Value
  }
  
  &lt;# Running Windows Vista without SP1 and earlier, i.e $Build is less than or equal to 6000 #&gt;
  elseif ($Build -le 6000) {
      $SysDrv = $Win32OS.SystemDrive
      $SysDrv = $SysDrv.Replace(":","$")
      $ProfDrv = "\\" + $Sender_IP + "\" + $SysDrv
      $ProfLoc = Join-Path -Path $ProfDrv -ChildPath "Documents and Settings"
      $Profiles = Get-ChildItem -Path $ProfLoc
      $LastProf = $Profiles | ForEach-Object -Process {$_.GetFiles("ntuser.dat.LOG")}
      $LastProf = $LastProf | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1
      $userId = $LastProf.DirectoryName.Replace("$ProfLoc","").Trim("\").ToUpper()
  }
  
  else{
      $userId = "Unknown/UserID"
  }
  
  
  # Logic block to return the correct value
  if ($userId -ne $NULL) {
      return "userId=" + $userId
  }
  elseif ($userID -eq $NULL)
  {
      return "userId=" + $userId
  }
  </code></pre>
  
  <p>I am willing to use other functions and cmdlets, but it is absolutely necessary that the script automatically runs in elevated mode because a third party program is calling script 1, which calls script 2, so script 2 needs to send value back to script 1, which goes to third party program.</p>
  
  <p>(I posted updated question in a question, <em><a href="https://stackoverflow.com/questions/18540505">ProcessStartInfo and Process in PowerShell - Authentication Error</a></em>).</p>