0

I'd like to run a Power shell script on start up that will query the local hostname and rename a local file by inserting the result.

For example, get hostname using something like:

$(Get-WmiObject Win32_Computersystem).name

..and insert the result into a file named and located as below,

C:\output\file-from-`Insert hostname`.txt

Its using the result of the Get-WmiObject to rename the existing file that I'm stuck with.

Any help would be great.

:-)

Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37
Veryape
  • 3
  • 2
  • 1
    So, something like `Rename-Item -Path "C:\output\file-from-Insert hostname.txt" -NewName "C:\output\file-from-$($env:COMPUTERNAME).txt"`? – notjustme May 18 '22 at 19:08
  • As an aside: The CIM cmdlets (e.g., `Get-CimInstance`) superseded the WMI cmdlets (e.g., `Get-WmiObject`) in PowerShell v3 (released in September 2012). Therefore, the WMI cmdlets should be avoided, not least because PowerShell (Core) v6+, where all future effort will go, doesn't even _have_ them anymore. Note that WMI still _underlies_ the CIM cmdlets, however. For more information, see [this answer](https://stackoverflow.com/a/54508009/45375). – mklement0 May 18 '22 at 21:43
  • Thanks @notjustme thats done the job. As soon as I figure out how to mark that as the answer I will. :-) – Veryape May 19 '22 at 13:07
  • I added a proper answer since it solved your problem. Comments can't be marked as the solution to a problem, only answers can (as far as I know). – notjustme May 19 '22 at 14:40

1 Answers1

0

This should do the trick.

Rename-Item -Path "C:\output\file-from-Insert hostname.txt" -NewName "C:\output\file-from-$($env:COMPUTERNAME).txt"
notjustme
  • 2,376
  • 2
  • 20
  • 27