PowerShell tutorial – Creating text (log) file in file system – exploring New-Item CMDLET

PowerShell - Creating new file using - New-Item
PowerShell - Creating new file using - New-Item

Hi All,

Greetings for the day!!! Today new learning so sharing ! SHARING IS CARING 🙂

Also keen to include as much as PowerShell stuff on Knowledge-Junction – making PowerShell Hub – All PowerShell learning at one place

Details / Background / Use Case

  • I am working on my SharePoint online project where I need to write some logs to text file from PowerShell script
  • I need to write logs either .log file or .txt file
  • We have PowerShell script to perform job
    • Script suppose to read SharePoint list
    • Based on details from SharePoint list, creating a modern communication site
    • And few other properties like – adding owners, hiding site title and so on
  • So need to maintain log for every functionality / operation – whether it succeeded or any error occurred
  • If there is any error then writing exception in error log file
  • Requirement is to maintain log files for success and exceptions for each site and details

Because of maintaining log file on file system I need to check how to create file on file system using PowerShell and write the content into it

In this article we will discuss how to create file on file system using PowerShell

Creating file using PowerShell

  • For creating text file we are using New-Item CMDLET
  • SYNTAX
New-Item
   [-Path] <String[]>
   -Name <String>
   [-ItemType <String>]
   [-Value <Object>]
   [-Force]
   [-Credential <PSCredential>]
   [-WhatIf]
   [-Confirm]
   [<CommonParameters>]

  • Basically it creates new item based on value specified in ItemType property
  • Following are the possible values for ItemType properties
    • File
    • Directory
    • SymbolicLink
    • Junction
    • HardLink
  • Also, type of item that can be created depend on the location of an item
    • In file system, New-Item create files
    • In registry, New-Item creates registries and entries
  • Example – Creating a log file on given path

New-Item -Name “mysite.log” -ItemType “File”

fig : PowerShell - creating "mysite.log" text file using New-Item CMDLET
fig : PowerShell – creating “mysite.log” text file using New-Item CMDLET
  • If we will navigate on respective file we will see the empty file created as
PowerShell - "mysite.log" text file created successfully using Neme-Item CMDLET
fig : PowerShell – “mysite.log” text file created successfully using Neme-Item CMDLET
  • If we create file with same name then will get an error as

New-Item -Name "mysite.log" -ItemType "File"

fig : PowerShell - exception / error while creating file with same name
fig : PowerShell – exception / error while creating file with same name
  • We can avoid the above exception / error with “-Force” attribute as

New-Item -Name “mysite.log” -ItemType “File” -Force

fig : PowerShell - "mysite.log" text file created successfully using Neme-Item CMDLET
fig : PowerShell – “mysite.log” text file created successfully using Neme-Item CMDLET
  • We can also specify initial content in file with “-Value” attribute as

New-Item -Name "mysite.log" -ItemType "File" -Value "LIFE IS BEAUTIFULE WITH POWERSHELL"

fig : PowerShell - "mysite.log" file successfully created with initial content - "LIFE IS BEAUTIFUL"
fig : PowerShell – “mysite.log” file successfully created with initial content – “LIFE IS BEAUTIFUL”
  • Please notice the length attribute, it shows the file have content
  • Lets open the file
fig : PowerShell – new file created with some content – New-Item -Value “LIFE IS BEAUTIFULE WITH POWERSHELL”

In next article of PowerShell we will discuss how to write content to file

REFERENCES

Thanks for reading! HAVE a FANTASTIC LEARNING!! LIFE IS BEAUTIFUL 🙂

Prasham Sabadra

LIFE IS VERY BEAUTIFUL. ENJOY THE WHOLE JOURNEY :) Founder of Microsoft 365 Junction, Speaker, Author, Learner, Developer, Passionate Techie. Certified Professional Workshop Facilitator / Public Speaker. Believe in knowledge sharing. Around 20+ years of total IT experience and 17+ years of experience in SharePoint and Microsoft 365 services Please feel free me to contact for any SharePoint / Microsoft 365 queries. I am also very much interested in behavioral (life changing) sessions like motivational speeches, Success, Goal Setting, About Life, How to live Life etc. My book - Microsoft 365 Power Shell hand book for Administrators and Beginners and 100 Power Shell Interview Questions - https://www.amazon.in/Microsoft-Administrators-Beginners-Interview-Questions/dp/9394901639/ref=tmm_pap_swatch_0?_encoding=UTF8&qid=1679029081&sr=8-11

You may also like...

1 Response

  1. August 14, 2023

    […] We have detailed article on how to create a file (either empty or with initial content) – PowerShell tutorial – Creating text (log) file in file system – exploring New-Item CMDLET […]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Microsoft 365

Subscribe now to keep reading and get access to the full archive.

Continue reading