Powershell add-type update my first script

Filip 831 Reputation points
2021-06-26T15:47:38.157+00:00

Hello everybody.

I tried this:

$code = @"  
using System;  
namespace HelloWorld  
{  
 public class Program  
 {  
 public static void Main(){  
 Console.WriteLine("Hello world!");  
 }  
 }  
}  
"@  
   
Add-Type -TypeDefinition $code -Language CSharp  
iex "[HelloWorld.Program]::Main()"  

It work.
But when I change the "$code" variable script and "add-type" it secend time it show me an error.

109499-a.png

Why i can't update secend time my script?
How can i do it?

Thanks for help.

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,361 questions
0 comments No comments
{count} votes

1 additional answer

Sort by: Most helpful
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 29,571 Reputation points Microsoft Vendor
    2021-06-28T08:40:27.32+00:00

    Hi,

    You have to use another type name because it must be unique within a session and can neither be updated nor removed.

    Best Regards,
    Ian Xue

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

    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.

    1 person found this answer helpful.
    0 comments No comments