how to convert vbs to powershell?

Dmitry Ivanov 0 Reputation points
2023-04-26T07:09:08.1133333+00:00

Set objSysInfo = CreateObject("ADSystemInfo") strUser = objSysInfo.UserName Set objSysInfo = CreateObject("ADSystemInfo") strUser = objSysInfo.UserName Set objUser = GetObject("LDAP://" & strUser) objUser.GetInfo strMail = objUser.Get("mail") Wscript.Echo strMail

PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,640 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Limitless Technology 44,431 Reputation points
    2023-04-26T12:01:35.78+00:00

    Hi, I'd be happy to help you out with your question. Sorry for the inconvenience caused. To convert the given VBScript code to PowerShell, we need to make some changes to the syntax and object model used in the script. Here is the equivalent PowerShell code: $objSysInfo = New-Object -ComObject "ADSystemInfo" $strUser = $objSysInfo.UserName $objUser = [ADSI]"LDAP://$strUser" $objUser.GetInfo() $strMail = $objUser.Get("mail") Write-Output $strMail In this code, we use the New-Object cmdlet to create a new instance of the ADSystemInfo COM object, which is used to retrieve the current user's name. Then, we use the [ADSI] type accelerator to connect to the user object in Active Directory using its LDAP path. Once we have the user object, we call the GetInfo() method to retrieve the object's properties, and the Get() method to retrieve the user's email address. Finally, we use the Write-Output cmdlet to display the email address on the console. If you have any other questions or need assistance with anything, please don't hesitate to let me know. I'm here to help.

    If the reply was helpful, please don’t forget to upvote or accept as answer, thank you.

    0 comments No comments

  2. Rich Matheisen 47,056 Reputation points
    2023-04-26T15:40:06.4833333+00:00

    This is a good place to start: https://learn.microsoft.com/en-us/previous-versions/windows/it-pro/windows-powershell-1.0/ee221101(v=msdn.10)?redirectedfrom=MSDN

    Before you start, though, ask yourself why you want to do this. You're only going to end up doing the same thing, just in a different language, and you're going to spend a LOT of time translating, and a lot more debugging the result, to accomplish that.

    If you don't mind starting the 32-bit version of PowerShell, look into the MSScriptControl.ScriptControl COM control. Feed your VB code into it and then run it.


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.