1

I create a batch file with:

%computername% > %computername%.txt

It works great and creates the text file with computer name. But how to create the same in PowerShell?

Different codes do not work, or my ideas are wrong. Only the computer name is in the text file but I wish to use the computer name for my text file if I don't know the name.

I tried:

Hostname >> .txt

But this writes the hostname in a file named .txt. Next try:

$env:computername > $env:computername.txt

But this does nothing.

I don't know what I can do?

1 Answer 1

1

I found a solution. This writes a text file in the desired path. To get the path of the current directory, use the wildcard .\. Here it is:

$hn = Hostname 
new-item .\$hn.txt 
$hn >> .\$hn.txt
  1. Create variable
  2. Create a text file
  3. Write to the text file

My problem is I tried all scripts without wildcard for path.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.