Filehost.ro - gazduire fisiere
Mrrrr's Forum (VIEW ONLY)
Un forum care ofera solutii pentru unele probleme legate in general de PC. Pe langa solutii, aici puteti gasi si alte lucruri interesante // A forum that offers solutions to some PC related issues. Besides these, here you can find more interesting stuff.
Nou pe simpatie:
barbyDana pe Simpatie.ro
Femeie
25 ani
Bacau
cauta Barbat
28 - 46 ani
Mrrrr's Forum (VIEW ONLY)ReguliInregistrareLoginPozeNu sunteti logat. Lista Forumurilor Pe Tematici
Mrrrr's Forum (VIEW ONLY) / Tutoriale si Ghiduri Utile // Tutorials and useful guides /

[WINDOWS] Create Your Custom PowerShell Commands

Pagini: 1 Moderat de TRaP, TonyTzu
pus acum 24 ore#1
Mrrrr
AdMiN
Postari: 2392
Open Powershell.

For this you need to save a PROFILE. By default, as far as I know, this is not created. So a command like the one below should return False

Test-Path $PROFILE

You can create a profile with the command below

New-Item -Path $PROFILE -ItemType File -Force

That will return the location of the ps1 file that gets created, and it's name, which should be something like:

Code:

Directory: C:\Users\USERNAME\Documents\WindowsPowerShell

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----        26.09.2026     10:05              0 Microsoft.PowerShell_profile.ps1

First, check if you can load Powershell profiles on your PC - normally, without prior setup, this should not be allowed for security/safety reasons. So the command below should return the word Undefined for all Scopes:

Get-ExecutionPolicy -List

To allow the current user to run ps1 files created locally, but not allow scripts downloaded from the internet to be run seamlessly unless explicitly unblocked and not allow all users on the computer run ps1 files, you can use the command below:

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned

You can always reset it to Undefined, or set it to Restricted, with a similar command replacing RemoteSigned with Undefined or Restricted, of course
You can then open the file above - Microsoft.PowerShell_profile.ps1 - in notepad with the command

notepad $PROFILE

You can then add functions to your profile, as many as you like. For example, I needed one to get folder sizes for a specific setup I have, so I added the function below to my profile, and then I can run it wherever I want using the name of the function which is Get-FolderSizes to display the results, or Get-FolderSizes -Save to save the results to CSV file in the location where I run the command

function Get-FolderSizes {
    param(
        [switch]$Save
    )

    $currentPath = (Get-Location).Path

    $results = foreach ($folder in Get-ChildItem -LiteralPath $currentPath -Directory) {

        # Only expand folders whose name contains "KEY_WORD"
        if ($folder.Name -like '*KEY_WORD*') {

            $subfolders = @(Get-ChildItem -LiteralPath $folder.FullName -Directory)

            # If there are subfolders, show each one
            if ($subfolders.Count -gt 0) {

                foreach ($subfolder in $subfolders) {

                    $size = (Get-ChildItem -LiteralPath $subfolder.FullName -File -Recurse -Force -ErrorAction SilentlyContinue |
                        Measure-Object -Property Length -Sum).Sum

                    [PSCustomObject]@{
                        'First Folder' = $folder.Name
                        'Subfolder'    = $subfolder.Name
                        'Size (GB)'    = [math]::Round($size / 1GB, 2)
                    }
                }

            } else {

                # KEY_WORD folder with no subfolders
                $size = (Get-ChildItem -LiteralPath $folder.FullName -File -Recurse -Force -ErrorAction SilentlyContinue |
                    Measure-Object -Property Length -Sum).Sum

                [PSCustomObject]@{
                    'First Folder' = $folder.Name
                    'Subfolder'    = ''
                    'Size (GB)'    = [math]::Round($size / 1GB, 2)
                }
            }

        } else {

            # Normal first-level folder: calculate its total size
            $size = (Get-ChildItem -LiteralPath $folder.FullName -File -Recurse -Force -ErrorAction SilentlyContinue |
                Measure-Object -Property Length -Sum).Sum

            [PSCustomObject]@{
                'First Folder' = $folder.Name
                'Subfolder'    = ''
                'Size (GB)'    = [math]::Round($size / 1GB, 2)
            }
        }
    }

    # Display table
    $results | Format-Table -AutoSize

    # Optional CSV export
    if ($Save) {
        $csvPath = Join-Path $currentPath 'FolderSizes.csv'
        $results | Export-Csv -Path $csvPath -NoTypeInformation -Encoding UTF8
        Write-Host "`nSaved to: $csvPath"
    }
}

Save and close the profile .ps1 file from Notepad itself and you are ready to go.
When you want to run a custom function, you will need to load the profile using the following command:

. $PROFILE

Then simply input the desired function name:

Get-FolderSizes -Save

Source: ChatGPT


_______________________________________


 
   
Pagini: 1