Creating Files and Folders

To create new objects with Windows PowerShell, you can use the New-Item cmdlet and specify the type of item you want to create, such as a directory, file or registry key. For example, this command creates a folder:

New-Item -Path '\\fs\Shared\NewFolder' -ItemType Directory

And this command creates an empty file:

New-Item -Path '\\fs\Shared\NewFolder\newfile.txt' -ItemType File

If you need to create a file and write data to it, there are at least two built-in methods. The first is to use the Out-File cmdlet:

$text = 'Hello World!' | Out-File $text -FilePath C:\data\text.txt

To overwrite an existing file, use the –Force switch parameter.

Alternatively, you can create files using the Export-Csv cmdlet, which exports the output to a csv file that can be opened in Excel:

Get-ADuser -Filter * | Export-Csv -Path C:\data\ADusers.csv