Files in PowerShell

Contents
Introduction
New-Item: create new file
Create new file with echo
Copy-Item: copy file
Remove-Item: delete file
Get-Content -Tail: file ending
Get-FileHash: control sum
Related Articles

New-Item

Example of creating a config.yml file with New-Item command

New-Item -Path . -Name "config.toml" -ItemType "file"

Example of creating a PowerShell configuration file

New-Item -Path $profile -Force -ItemType "file"

Create file with echo

echo $null >> new_file.txt

Copy-Item

To copy a file from the current directory to a nested one, run

Copy-Item -Path "file_name" -Destination "имя_директории"

For example

Copy-Item -Path "requirements.txt" -Destination "autotest_distr"

Banner Image

Remove-Item

You can delete a file with the Remove-Item command

Delete all files from the directory C:\Test which have a dot in their name

Remove-Item C:\Test\*.*

A script that deletes a Python .exe file from WinodwsApps

# DeletePythonFromWindowsApps.ps1 $CURRENT_USER = $Env:Username $PYPATH = 'C:\Users\' + $CURRENT_USER + '\AppData\Local\Microsoft\WindowsApps\python.exe' $PY3PATH = 'C:\Users\' + $CURRENT_USER + '\AppData\Local\Microsoft\WindowsApps\python3.exe' If (Test-Path -path ${PYPATH}) { Remove-Item $PYPATH } If (Test-Path -path ${PY3PATH}) { Remove-Item $PY3PATH }

Banner Image

Get-Content

You only need to look at the end of the file by analogy with tail in Linux you can use Get-Content with the -Tail flag.

For example, to get the last 100 lines of a file 2024-09-15.log

Get-Content -Path "C:\logs\2024-09-15.log" -Tail 100

If you need the real time log, use the -Wait flag

Get-Content -Path "C:\logs\2024-09-15.log" -Wait

You can combine -Wait and -Tail and monitor only the last lines in real time

Get-Content -Path "C:\logs\2024-09-15.log" -Wait -Tail

Get-FileHash

To check the checksum of a file in PowerShell, just run

Get-FileHash путь_до_файла -Algorithm MD5

This is an analogue Bash md5sum commands in Linux and md5 in OpenBSD

Check the notes.txt file in the current directory

Get-FileHash notes.txt -Algorithm MD5

--------- ---- ---- MD5 14B17234E237534421B6492B8D757507 C:\Users\andrei\Downlo…

Related Articles
Windows
PowerShell
Install
Basics
Alias
Functions
Network
Users
Files
REST API requests
Errors

Search on this site

Subscribe to @aofeed channel for updates

Visit Channel

@aofeed

Feedbak and Questions in Telegram

@aofeedchat