Powershell to work with text files

Going back to another problem, I've decided that in order to create VMs on-the-fly, I'm going to need a list of MAC-Addresses which are already in our DHCP server. This will mean not needing to use the netsh command to populate DHCP, and will give us a little more control.

So I need a text file containing all the MAC's which are DHCP enabled. I need to use the first MAC in the file, then delete it from the file (so it doesnt get used again). Powershell to the rescue yet again....and I can even use this within my VM creation script to streamline the whole process.

In the following example the input text file is called c:\test.txt and contains a basic list.

-----------------------------------------------------
#load the file into a string called file
$file=get-content "C:\test.txt"

#$file[0] represents the first line of data......do with as your wish

delete the data which was in the first line
$file[0]=""

#output it to a tempfile
$file out-file "C:\tempfile.txt"

#delete the original file
Remove-Item c:\test.txt

#delete any empty lines from the temp file, and save it as the original name
cat c:\tempfile.txt where {$_ -notmatch "^$" } > c:\test.txt

-----------------------------------------------------

0 comments: