You’re right. That’s exactly what you need to do. This forum is not designed to provide individual tutoring from scratch. To my knowledge it’s intended to provide mutual help to/from folks who have done some book/video/other PowerShell training.
Recommendation
- Use the PowerShell cmdlets and functions by exploring their parameters, what kind of input they expect, and what kind of output they return.
Stay away from advanced features like using methods because that requires knowledge and understanding of the underlying .Net classes
Master the parameters first. Do all your tasks using the parameters. Only when you have a task that cannot be done with parameters, you will need to look deeper.
For example, to create a file you simply use the Out-File cmdlet as in
'test text 1' | out-file .\myFile1.txt
You can also use other cmdlets depending on the file content, such as export-csv or Export-Clixml
Now to your specific question.
That’s not entirely accurate.
Get-ChildItem returns objects of type System.IO.FileInfo
Now the System.IO.FileInfo .Net class FileInfo Class (System.IO) | Microsoft Learn has a .Create method FileInfo.Create Method (System.IO) | Microsoft Learn
Example of using this .NET class to create and write to a file may go something like
$File = [System.IO.File]::Create('c:\data\scripts\myfile2.txt')
$myTextAsByteArray = [System.Text.Encoding]::UTF8.GetBytes('hello world')
$File.Write($myTextAsByteArray,0,$myTextAsByteArray.Count)
$File.Close()
You can see the $File type being System.IO.Stream
$File.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False FileStream System.IO.Stream
and the file content
Get-Content .\myfile2.txt
hello world
Notice that using .NET methods to create a file and write to it required:
- Instantiating a Stream object as in: $File = [System.IO.File]::Create(‘c:\data\scripts\myfile2.txt’)
- Converting the text to be written to the file to a byte array which is required for the Stream .Create method as in: $myTextAsByteArray = [System.Text.Encoding]::UTF8.GetBytes(‘hello world’)
- Writing the text to the file specifying the start offset and byte count as in: $File.Write($myTextAsByteArray,0,$myTextAsByteArray.Count)
- Closing the file as in: $File.Close()
Not to mention understanding and knowing lots of details about System.IO.FileMode, System.IO.FileAccess, System.IO.FileShare, and buffersize.
Not understanding all that can have negative consequences. For example:
(Get-ChildItem).Create()
will lock all files in the current folder such that you cannot read or write to any of them. This is because under the covers the .Create method of the System.IO.Stream was invoked with the default System.IO.FileShare value of zero which means None. You can only get back to your files by killing the PowerShell.exe or PowerShell_ISE.exe process under which you invoked the ill-advised command (Get-ChildItem).Create() which now has a lock on your files.
Long story short, simply use out-file, stay away from methods and advanced features until you master the standard parameters. Walk you before you run son.