Powershell Tutorial
Introduction to Powershell Managing History of Powershell Commands Cmdlet, Alias, Function and Module in Powershell PSDrive Profile in Windows Powershell Variable Management in Powershell Passing values to parameters from file Creating files and directories Basic File System commands Finding the patterns in files Unique command Replacing patterns in files Comparing objects Sorting objects Other object commands Getting members of an Object Managing processes Managing Jobs IO redirection Piping System Commands Network commands Service Commands WMI(Windows Management Instrumentation) CIM (Common Information Model) Formatting output exporting output XML processing Powershell Module Management Remote computer managementcreating files and directories in windows powershell
Creating files Simplest way to create a file in Powershell is to use below command.
> notepad abc.txt
We can also create file using below commands.- echo “xyz” > abc.txt : This command will create new file abc.txt if it does not exist. If it exists, contents will be overwritten.
- echo “xyz” >> abc.txt : This command will append the data to existing file.
- Get-Process > process.txt : This command will store the process information in the file “process.txt”. Note that we can store the output of any command in a file using this syntax.
dir | Out-File abc.txt
You can also use below command to create new file.
New-Item f1.txt -type file -force -value “You can also put this data in a file”
“Set-Content” command also allows you to create new file. Note that file is over-written if it already exists. In below example, file “f1.txt” is created with content as “hi how are you”.
Set-Content f1.txt “hi how are you”
Appending to file You can use below syntax to append to file using ASCII encoding
dir | Out-File abc.txt -append -Encoding Ascii
You can also use below command to append data to file.
Add-Content <file-name> <data-to-be-appended>
Creating directories To create a directory, you need to use below command with type as “directory”.
New-Item xyz -type directory
Web development and Automation testing
solutions delivered!!