• PowerShell loop through JSON File
    20 August

    Loop Through JSON File in PowerShell

    Using ConvertFrom-Json Cmdlet We can use the ConvertFrom-Json cmdlet to traverse the JSON file with first and nested-level keys; Let’s explore them below. Traverse JSON File Containing First-Level Keys Use ConvertFrom-Json with the foreach loop to traverse the JSON file having first-level keys only. [crayon-662d410dac0d8970244429/] [crayon-662d410dac0e4253229066/] [crayon-662d410dac0e6746880726/] We used the Get-Content cmdlet to read the […]

  • PowerShell find filename by name
    29 June

    PowerShell Find File by Name

    Using Get-ChildItem Cmdlet Use the Get-ChildItem cmdlet with -Path and -Filter parameters to find the specified files and directories in the given path in PowerShell. [crayon-662d410dac6d5523035112/] [crayon-662d410dac6dd036495298/] The Get-ChildItem retrieves items and child items from one or multiple paths (locations). The -Path parameter is used to specify one or multiple paths to locations; we can […]

  • PowerShell get last modified file in directory
    24 June

    PowerShell Get Last Modified File in Directory

    For this article, our working directory will be C:\Test1 containing two files named File1.txt and File2.txt. Using Get-ChildItem along with Sort-Object Cmdlet Use the Get-ChildItem cmdlet with Sort-Object` cmdlet to get last modified file in directory in PowerShell. Sort-Object cmdlet will sort the File object by LastWriteTime property. [crayon-662d410dacb8e705730768/] [crayon-662d410dacb96662399025/] The code uses the Get-ChildItem […]

  • Pass multiple parameters to Powershell script
    17 June

    Pass Multiple Parameters to PowerShell Script

    All the code snippets are written in the MyScript.ps1 file; you can name this file to any name of your choice. Using Automatic Variable $args Use automatic variable $args to pass multiple parameters to PowerShell script. [crayon-662d410dace39765806212/] [crayon-662d410dace3f097240847/] [crayon-662d410dace41528548792/] The args variable is an array that contains the value of all the parameters passed to […]

  • PowerShell compare lastwritetime of two files
    06 May

    PowerShell Compare LastWriteTime of Two Files

    Using LastWriteTime property To compare LastWriteTime of two files in PowerShell: Use Get-Item to get items. Use the if-else block to compare LastWriteTime of both files. [crayon-662d410dad1f1362197463/] [crayon-662d410dad1f9770354760/] Using Compare-Object Cmdlet To compare the LastWriteTime of two files in PowerShell: Use Get-Item to get items. Use the Compare-Object cmdlet to compare the LastWriteTime of two […]

  • Append data to file using PowerShell
    03 May

    Append Data to File using PowerShell

    Appending Text to a Text File We have various approaches to append content in a text file using PowerShell. For example, we can append information on the same line, on a new line with/without special characters, on multiple lines, and append formatted data such as tabular content. But, before moving towards possible solutions, it is […]

  • Convert XLSX to CSV in PowerShell
    23 April

    Convert XLSX to CSV in PowerShell

    The xlsx file can be easily converted into csv using the following methods: Before converting a file, please ensure that your PowerShell is opened in the administrative mode, and the execution policy should not be set to Restricted. [crayon-662d410dad90d852571733/] [crayon-662d410dad911395985690/] In the above code, we used the Get-ExecutionPolicy command to check if the execution policy […]

  • Convert CSV to XLSX in PowerShell
    23 April

    Convert CSV to XLSX in PowerShell

    The csv file can be easily converted into xlsx using the following methods: Before converting files, please verify that your PowerShell is opened in administrative mode and that the execution policy is not set to Restricted. [crayon-662d410dada1c397787695/] [crayon-662d410dada1f395391759/] The above output demonstrates that the execution policy is set to Restricted. So, if the current execution […]

  • PowerShell check if directory is empty
    21 April

    PowerShell Check if Directory is Empty

    For this article, we are using three directories, E:\Test\Script Files, E:\Test\Test Files, and E:\Test\Sample Files. The E:\Test\Script Files directory is not empty and contains test.bat and testPS.ps1 files, while the E:\Test\Test Files directory is empty because it contains nothing, neither files nor folders The E:\Test\Sample Files directory is also not empty; it contains file.txt, file1.txt, […]