diff --git a/src/readme.graph.md b/src/readme.graph.md
index 8d7b62ec83f..94fe7df6792 100644
--- a/src/readme.graph.md
+++ b/src/readme.graph.md
@@ -579,7 +579,8 @@ directive:
let odataNextLinkRegex = /(^\s*)(while\s*\(\s*_nextLink\s*!=\s*null\s*\))/gmi
if($.match(odataNextLinkRegex)) {
// Add custom -PageSize parameter to *_List cmdlets that support Odata next link.
- $ = $.replace(odataNextLinkRegex, '$1while (_nextLink != null && this.ShouldIteratePages(this.InvocationInformation.BoundParameters, result.Value.Length))\n$1');
+ let initializePageCountPlaceholder = 'this.InitializePageCount(result.Value.Length);'
+ $ = $.replace(odataNextLinkRegex, `$1${initializePageCountPlaceholder}\n$1while (_nextLink != null && this.ShouldIteratePages(this.InvocationInformation.BoundParameters, result.Value.Length))$1`);
let psBaseClassImplementationRegex = /(\s*:\s*)(global::System.Management.Automation.PSCmdlet)/gmi
$ = $.replace(psBaseClassImplementationRegex, '$1Microsoft.Graph.PowerShell.Cmdlets.Custom.ListCmdlet');
diff --git a/tools/Custom/ListCmdlet.cs b/tools/Custom/ListCmdlet.cs
index 13660faf15f..5000e3069b0 100644
--- a/tools/Custom/ListCmdlet.cs
+++ b/tools/Custom/ListCmdlet.cs
@@ -48,11 +48,6 @@ public partial class ListCmdlet : global::System.Management.Automation.PSCmdlet
[global::System.Management.Automation.Alias("CV")]
public string CountVariable { get => this._countVariable; set => this._countVariable = value; }
- ///
- /// Default number of items per page.
- ///
- internal const int DefaultPageSize = 100;
-
///
/// Maximum number of items per page.
///
@@ -101,28 +96,25 @@ public void InitializeCmdlet(ref global::System.Management.Automation.Invocation
null));
}
- // Move `-Top` parameter to `limit`.
if (invocationInfo.BoundParameters.ContainsKey("Top"))
{
- limit = top;
- }
-
- int currentPageSize = invocationInfo.BoundParameters.ContainsKey("PageSize") ? PageSize : DefaultPageSize;
- if (invocationInfo.BoundParameters.ContainsKey("Top") && limit < currentPageSize)
- {
- currentPageSize = limit;
- }
+ if ((top > MaxPageSize) || invocationInfo.BoundParameters.ContainsKey("All") || invocationInfo.BoundParameters.ContainsKey("PageSize"))
+ {
+ limit = top;
+ }
- if (invocationInfo.BoundParameters.ContainsKey("PageSize") || invocationInfo.BoundParameters.ContainsKey("Top") || invocationInfo.BoundParameters.ContainsKey("All")){
- // Explicitly set `-Top` parameter to currentPageSize in order for the generated cmdlets to construct a URL with a `$top` query parameter.
- invocationInfo.BoundParameters["Top"] = currentPageSize;
- top = currentPageSize;
- }
+ if (top > MaxPageSize)
+ {
+ // Remove $top from query parameters, we will handle paging.
+ top = default;
+ invocationInfo.BoundParameters.Remove("Top");
+ }
- if (limit != default)
- {
- requiredPages = limit / currentPageSize;
- overflowItemsCount = limit % currentPageSize;
+ if (invocationInfo.BoundParameters.ContainsKey("PageSize"))
+ {
+ invocationInfo.BoundParameters["Top"] = PageSize;
+ top = PageSize;
+ }
}
if ((!invocationInfo.BoundParameters.ContainsKey("Count")) && invocationInfo.BoundParameters.ContainsKey("CountVariable"))
@@ -133,6 +125,15 @@ public void InitializeCmdlet(ref global::System.Management.Automation.Invocation
}
}
+ public void InitializePageCount(int initialPageSize)
+ {
+ if (limit != default && initialPageSize != default)
+ {
+ requiredPages = limit / initialPageSize;
+ overflowItemsCount = limit % initialPageSize;
+ }
+ }
+
///
/// Determines whether the cmdlet should follow the OData next link URL.
/// Iteration will only occur when limit/top is not set, or if there a more items to fetch.
@@ -166,7 +167,7 @@ public bool ShouldIteratePages(global::System.Collections.Generic.Dictionary