none
Powershell Create New File RRS feed

  • Question

  • Hello everyone ,

    I have a script to create a file "M_VAR.cmd" and when i execute this file I have a strange error.

    My Powershell script 

    The script is ok 

    My file exist but ...

    When i execute my script there is a strange error ...  


    And in my file there is nothing strange ... 

    Do you have an answer ? 

    Thanks for your help 


    • Edited by Twenty94470 Thursday, October 13, 2016 4:24 PM
    Thursday, October 13, 2016 4:22 PM

Answers

  • Try:

    "SET test='tttt'" | Out-File -FilePath .\M_VAR.cmd -Encoding ascii
    "echo %test%" | Out-File -FilePath .\M_VAR.cmd -Encoding ascii -Append

    Powershell is using Unicode as its default encoding. The Out-File cmdlet allows to use another encoding, here ASCII for later use in cmd.exe.

    wizend

    • Marked as answer by Twenty94470 Friday, October 14, 2016 7:53 AM
    Thursday, October 13, 2016 5:58 PM

All replies

  • What is the purpose of all of this?

    -- Bill Stewart [Bill_Stewart]

    Thursday, October 13, 2016 4:28 PM
    Moderator
  • Try:

    "SET test='tttt'" | Out-File -FilePath .\M_VAR.cmd -Encoding ascii
    "echo %test%" | Out-File -FilePath .\M_VAR.cmd -Encoding ascii -Append

    Powershell is using Unicode as its default encoding. The Out-File cmdlet allows to use another encoding, here ASCII for later use in cmd.exe.

    wizend

    • Marked as answer by Twenty94470 Friday, October 14, 2016 7:53 AM
    Thursday, October 13, 2016 5:58 PM
  • Your New-Item line is creating a Unicode text file. This cannot be read by the command interpreter.

    There are a number of solutions here but try this.

    "Set test=test" | Out-File C:\Windows\m_var.cmd -Encoding ascii

    Blog: http://scriptimus.wordpress.com

    Thursday, October 13, 2016 6:00 PM
    Moderator
  • Why do we need to create a text file containing a cmd.exe command that sets an environment variable? Why not just set the variable in PowerShell?

    -- Bill Stewart [Bill_Stewart]

    Thursday, October 13, 2016 10:34 PM
    Moderator
  • Hello Thanks everyone for your help !! 
    That's works fine ! 

    @Bill In my company all application profession call this file for variable, I prefere modify one file rather all software :p 

    Best regards !

    Friday, October 14, 2016 7:57 AM